Converting a URI path to a relative file system path in .NET

acdx picture acdx · Mar 14, 2010 · Viewed 61.4k times · Source

How do I convert an absolute or relative URI path (e.g. /foo/bar.txt) to a (segmentwise) corresponding relative file system path (e.g. foo\bar.txt) in .NET?

My program is not an ASP.NET application.

Answer

Ashish Gupta picture Ashish Gupta · Mar 14, 2010

Have you already tried Server.MapPath?
or Uri.LocalPath property? Something like following :

string uriString = "file://server/filename.ext";
// Lesson learnt - always check for a valid URI
if(Uri.IsWellFormedUriString(uriString))
{
    Uri uri = new Uri(uriString);
    Console.WriteLine(uri.LocalPath);
}