본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - 상대경로 구하기 / path ( MakeRelativeUri / Uri )

728x90
반응형


 WPF & C# - 상대경로 구하기 / path ( MakeRelativeUri / Uri )



url.zip


@ MainWindow.xaml


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<Window x:Class="url.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:url"

        mc:Ignorable="d"

        Title="MainWindow" Height="299.09" Width="482.575">

    <Grid>

        <Button x:Name="btn_relativePath" Content="relative_Path" HorizontalAlignment="Left" Margin="10,203,0,0" VerticalAlignment="Top" Width="85" Height="39" Click="btn_relativePath_Click"/>

        <Button x:Name="btn_Open01" Content="Open 01" HorizontalAlignment="Left" Margin="10,67,0,0" VerticalAlignment="Top" Width="85" Height="40" Click="btn_Open01_Click"/>

        <TextBlock x:Name="txtBk01" HorizontalAlignment="Left" Margin="100,77,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="365" Height="20"/>

        <Button x:Name="btn_Open02" Content="Open 02" HorizontalAlignment="Left" Margin="10,136,0,0" VerticalAlignment="Top" Width="85" Height="40" Click="btn_Open02_Click"/>

        <TextBlock x:Name="txtBk02" HorizontalAlignment="Left" Margin="100,146,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="365" Height="20"/>

        <TextBlock x:Name="txtBk03" HorizontalAlignment="Left" Margin="100,213,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="365" Height="20"/>

        <TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="상대경로 구하기" VerticalAlignment="Top" FontSize="24"/>

    </Grid>

</Window>

 

Colored by Color Scripter

cs



@ MainWindow.xaml.cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace url
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        
        private void btn_Open01_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = "";   // 기본 파일명
            dlg.DefaultExt = "";   // 기본 확장자
            dlg.Filter = "확장자에 대한 설명|*.*"// 확장자 필터
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true) txtBk01.Text = dlg.FileName;
        }
 
        private void btn_Open02_Click(object sender, RoutedEventArgs e)
        {
            // 파일 열기
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = "";    // 기본 파일명
            dlg.DefaultExt = "";   // 기본 확장자
            dlg.Filter = "확장자에 대한 설명|*.*"// 확장자 필터
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true) txtBk02.Text = dlg.FileName;
        }
 
        private void btn_relativePath_Click(object sender, RoutedEventArgs e)
        {
            if (txtBk01.Text != "" && txtBk02.Text != "")
            {
                Uri relative_Path = new Uri(txtBk01.Text).MakeRelativeUri(new Uri(txtBk02.Text));
                txtBk03.Text = relative_Path.ToString();
            }
        }
    }
}
 
cs


 @ 상대경로를 구하는 함수이다.

저런 함수가 있는지도 모르고 항상 계산을 했었다.

역시... 모르면 손발이 고생이다.

혼자 한다면 항상 인터넷이 곧 스승인듯하다. ^^


만약 다른 방식으로 두 폴더를 비교해넣었는데, 값이 좀 이상하다 싶으면 경로명 마지막에 + "\\" 를 추가해 넣어주면 된다.


[ OpenFileDialog ]

https://technet.microsoft.com/ko-kr/library/system.windows.controls.openfiledialog(v=vs.95)




http://insurang.tistory.com



728x90
반응형