첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

▶ Uri 클래스 : MakeRelativeUri 메소드를 사용해 상대 디렉토리 경로 구하기 예제

using System;

string relativeDirectoryPath = GetRelativeDirectoryPath(Environment.CurrentDirectory, "d:\\temp");

Console.WriteLine(relativeDirectoryPath);

 

728x90

 

▶ Uri 클래스 : MakeRelativeUri 메소드를 사용해 상대 디렉토리 경로 구하기

using System;
using System.IO;

#region 상대 디렉토리 경로 구하기 - GetRelativePath(baseDirectoryPath, sourceDirectoryPath)

/// <summary>
/// 상대 디렉토리 경로 구하기
/// </summary>
/// <param name="baseDirectoryPath">기준 절대 디렉토리 경로</param>
/// <param name="sourceDirectoryPath">소스 절대 디렉토리 경로</param>
/// <returns>상대 디렉토리 경로</returns>
public string GetRelativeDirectoryPath(string baseDirectoryPath, string sourceDirectoryPath)
{
    if(string.IsNullOrWhiteSpace(baseDirectoryPath))
    {
        throw new ArgumentNullException("baseDirectoryPath IS NULL OR WHITESPACE STRING");
    }

    if(string.IsNullOrWhiteSpace(sourceDirectoryPath))
    {
        throw new ArgumentNullException("sourceDirectoryPath IS NULL OR WHITESPACE STRING");
    }

    if(Path.GetPathRoot(baseDirectoryPath) != Path.GetPathRoot(sourceDirectoryPath))
    {
        throw new ArgumentException("CAN NOT PROCESS AS DIFFERENT ROOT PATH");
    }

    if(!Path.IsPathRooted(baseDirectoryPath))
    {
        throw new ArgumentException("baseDirectoryPath IS NOT ABSOLUTE PATH");
    }

    if(!Path.IsPathRooted(sourceDirectoryPath))
    {
        throw new ArgumentException("sourceDirectoryPath IS NOT ABSOLUTE PATH");
    }
 
    baseDirectoryPath   = baseDirectoryPath.Trim();
    sourceDirectoryPath = sourceDirectoryPath.Trim();

    if(Directory.Exists(baseDirectoryPath + Path.DirectorySeparatorChar))
    {
        baseDirectoryPath = baseDirectoryPath + Path.DirectorySeparatorChar;
    }

    if(Directory.Exists(sourceDirectoryPath + Path.DirectorySeparatorChar))
    {
        sourceDirectoryPath = sourceDirectoryPath + Path.DirectorySeparatorChar;
    }
 
    Uri baseURI = new Uri(baseDirectoryPath);

    Uri sourceAbsoluteURI = new Uri(sourceDirectoryPath);

    Uri sourceRelativeURI = baseURI.MakeRelativeUri(sourceAbsoluteURI);

    string relativePath = Uri.UnescapeDataString(sourceRelativeURI.ToString());
 
    return relativePath.Replace('/', Path.DirectorySeparatorChar);
}

#endregion
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요