Convert the URI path to the relative file system path in .NET.

How to convert an absolute or relative URI path (e.g. /foo/bar.txt ) to (by segment) the corresponding relative file system path (e.g. foo\bar.txt ) in .NET?

My program is not an ASP.NET application.

+45
c # uri path
Mar 14 '10 at 12:44
source share
3 answers

Have you tried Server.MapPath ?
or Uri.LocalPath property? Something like the 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); } 
+73
Mar 14 '10 at 12:50
source share

I figured out this way to create the complete path of an absolute file system from a relative or absolute URI and base path.

FROM

 Uri basePathUri = new Uri(@"C:\abc\"); 

From relative URI:

 string filePath = new Uri(basePathUri, relativeUri).AbsolutePath; 

From an absolute URI:

 // baseUri is a URI used to derive a relative URI Uri relativeUri = baseUri.MakeRelativeUri(absoluteUri); string filePath = new Uri(basePathUri, relativeUri).AbsolutePath; 
+8
Mar 14 '10 at 15:19
source share

You can do it:

 var localPath = Server.MapPath("/foo/bar.txt"); 

See MSDN for details.

+4
Mar 14
source share



All Articles