는
당신은 주어진 하나에 대한 상대 부모 경로 계산하기 위해 다음과 같은 정적 인 방법을 사용할 수 있습니다
public static string GetRelativeParentPath(string basePath, string path)
{
return GetRelativePath(basePath, Path.GetDirectoryName(path));
}
public static string GetRelativePath(string basePath, string path)
{
// normalize paths
basePath = Path.GetFullPath(basePath);
path = Path.GetFullPath(path);
// same path case
if (basePath == path)
return string.Empty;
// path is not contained in basePath case
if (!path.StartsWith(basePath))
return string.Empty;
// extract relative path
if (basePath[basePath.Length - 1] != Path.DirectorySeparatorChar)
{
basePath += Path.DirectorySeparatorChar;
}
return path.Substring(basePath.Length);
}
을 그리고 이것은 당신이 그것을 사용할 수있는 방법입니다
static void Main(string[] args)
{
string basePath = @"c:\Projects\";
string file1 = @"c:\Projects\bin\file1.exe";
string file2 = @"c:\Projects\src\folder\file2.exe";
Console.WriteLine(GetRelativeParentPath(basePath, file1));
Console.WriteLine(GetRelativeParentPath(basePath, file2));
}
출력 :
bin
src\folder