计算相对于一些路径根 - Path.Combine的逆 [英] Calculating the path relative to some root- the inverse of Path.Combine
问题描述
有没有一种可靠的方法来计算Path.Combine()?
Is there a reliable way to calculate the inverse of Path.Combine()?
Path.Combine(C:\文件夹,子目录\ something.txt)可能会返回类似C:\文件夹\子目录\ something.text。我要的是相反的,一个函数,其中Path.GetRelativeUrl(C:\文件夹,C:\文件夹\子目录\ something.text)。将返回类似子目录\ something.txt
Path.Combine("c:\folder", "subdirectory\something.txt") might return something like "c:\folder\subdirectory\something.text". What I want is the inverse, a function where Path.GetRelativeUrl("c:\folder", "c:\folder\subdirectory\something.text") would return something like ""subdirectory\something.txt".
的一个解决方案是做字符串比较和修整根,但是这不会工作时相同的路径是前pressed以不同的方式(使用..或〜1在路径前pression)。
One solution is to do string comparisons and trim the roots, but this would not work when the same path is expressed in different ways (use of ".." or "~1" in path expression).
推荐答案
好吧,所以在我来说,我没有一些更严厉的边缘情况(FULLPATH和relativePath混合网络地图位置,超长文件名)。我最终什么做什么是创建以下
Alright so in my case I don't have some of the tougher edge cases (fullPath and relativePath mixing network map locations, super long filenames). What I ended up doing was creating the class below
public class PathUtil
{
static public string NormalizeFilepath(string filepath)
{
string result = System.IO.Path.GetFullPath(filepath).ToLowerInvariant();
result = result.TrimEnd(new [] { '\\' });
return result;
}
public static string GetRelativePath(string rootPath, string fullPath)
{
rootPath = NormalizeFilepath(rootPath);
fullPath = NormalizeFilepath(fullPath);
if (!fullPath.StartsWith(rootPath))
throw new Exception("Could not find rootPath in fullPath when calculating relative path.");
return "." + fullPath.Substring(rootPath.Length);
}
}
看来工作pretty的好。至少,它通过了这些测试NUnit的:
It seems to work pretty well. At least, it passes these NUnit tests:
[TestFixture]
public class PathUtilTest
{
[Test]
public void TestDifferencesInCapitolizationDontMatter()
{
string format1 = PathUtil.NormalizeFilepath("c:\\windows\\system32");
string format2 = PathUtil.NormalizeFilepath("c:\\WindowS\\System32");
Assert.AreEqual(format1, format2);
}
[Test]
public void TestDifferencesDueToBackstepsDontMatter()
{
string format1 = PathUtil.NormalizeFilepath("c:\\windows\\system32");
string format2 = PathUtil.NormalizeFilepath("c:\\Program Files\\..\\Windows\\System32");
Assert.AreEqual(format1, format2);
}
[Test]
public void TestDifferencesInFinalSlashDontMatter()
{
string format1 = PathUtil.NormalizeFilepath("c:\\windows\\system32");
string format2 = PathUtil.NormalizeFilepath("c:\\windows\\system32\\");
Console.WriteLine(format1);
Console.WriteLine(format2);
Assert.AreEqual(format1, format2);
}
[Test]
public void TestCanCalculateRelativePath()
{
string rootPath = "c:\\windows";
string fullPath = "c:\\windows\\system32\\wininet.dll";
string expectedResult = ".\\system32\\wininet.dll";
string result = PathUtil.GetRelativePath(rootPath, fullPath);
Assert.AreEqual(expectedResult, result);
}
[Test]
public void TestThrowsExceptionIfRootDoesntMatchFullPath()
{
string rootPath = "c:\\windows";
string fullPath = "c:\\program files\\Internet Explorer\\iexplore.exe";
try
{
PathUtil.GetRelativePath(rootPath, fullPath);
}
catch (Exception)
{
return;
}
Assert.Fail("Exception expected");
}
}
测试用例依靠现有的..这些文件在大多数Windows安装,但您的情况可能有所不同某些共同文件。
The test cases rely on certain files existing.. these files are common on most Windows installs but your mileage may vary.
这篇关于计算相对于一些路径根 - Path.Combine的逆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!