maandag 3 januari 2011

C# - using the Uri class to find the delta of 2 paths

I've seen a lot of solutions for calculating a delta between 2 paths and only recently found out how simple this is when using the Uri class of the .NET framework. The most important method here is Uri.MakeRelativeUri. The following example finds the path of files relative to the current directory:

foreach (string file in Directory.GetFiles(<directory>, <searchpattern>, SearchOption.AllDirectories))
{
    Uri uriFile = new Uri(Directory.GetCurrentDirectory()).MakeRelativeUri(new Uri(file));
    Console.WriteLine(Path.GetDirectoryName(uriFile.ToString()));
}
 

Geen opmerkingen:

Een reactie posten