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
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON] ServicePointManager 클래스 : DefaultConnectionLimit 정적 속성을 사용해 웹 요청 동시 접속 수 설정하기 (0) | 2019.10.12 |
---|---|
[C#/COMMON] 사용자 계정 로그온 권한 조사하기 (0) | 2019.10.11 |
[C#/COMMON] 한글 문자열 확장 처리 기능 사용하기 (0) | 2019.10.07 |
[C#/COMMON] WebClient 클래스 : 외부 IP 주소 구하기 (0) | 2019.10.06 |
[C#/COMMON] 공백 문자열 압축하기 (0) | 2019.10.06 |
[C#/COMMON] Uri 클래스 : MakeRelativeUri 메소드를 사용해 상대 디렉토리 경로 구하기 (0) | 2019.10.06 |
[C#/COMMON] Dictionary 클래스 : 이중 키 딕셔너리 사용하기 (0) | 2019.10.06 |
[C#/COMMON] BitConverter 클래스 : 빅 엔디안(Big Endien) 바이트 배열에서 정수 구하기 (0) | 2019.10.06 |
[C#/COMMON] 빅 엔디안(Big Endien) 바이트 배열 구하기 (0) | 2019.10.06 |
[C#/COMMON] 16진수 문자열 구하기 (0) | 2019.10.06 |
[C#/COMMON] Trace 클래스 : TraceInformation 정적 메소드를 사용해 로그 기록하기 (0) | 2019.10.06 |
댓글을 달아 주세요