tags:

views:

888

answers:

4

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.

+3  A: 

Check out Server.MapPath. http://msdn.microsoft.com/en-us/library/ms524632.aspx

If it's not an ASP.NET application, check out:

System.IO.Path.GetFullPath("filename")
dcp
Gives me a "Uri formats are not supported" error, in .Net 3.5
Binary Worrier
+2  A: 

You can do this:

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

See MSDN for details

Nick Craver
My program is not an ASP.NET application, how do I get access to the Server object?
acdx
@acdx - I'm confused then...relative to what, a running application? Your question is tagged `uri`, so I'm confused if it's not web, explain?
Nick Craver
I'd like to get a relative file path, such as `foo\bar.txt`, that I can pass to `Path.Combine` to create an absolute file path.
acdx
@acdx - Relative means in relation to...in relation to **what** is the question, you're asking about a URI, but saying it's not web...this makes no sense and needs some clarification. For starters, if not a web app, what **are** you running?
Nick Craver
My application is a regular .NET console application. I want to map a URI to a file system path. I thought the easiest way to do this would be to convert the URI path to a relative file system path and then pass it to `Path.Combine` along with a base path like `C:\abc\` to form an absolute file system path. About terminology, a relative path by definition doesn't contain information about what it relates to.
acdx
@acdx - But, the thing figuring out what it relates to...it's relative to that, and you left that out of the question. A URI can go 20 places, is it on the same machine, running IIS? If none of the answers already posted here help, I'm at a loss to figure out what you're doing...a more detailed question is in order. Try updating with an input and result example, that'd be much more helpful.
Nick Craver
"But, the thing figuring out what it relates to...it's relative to that, and you left that out of the question."Could you please rephrase that first sentence, I don't understand it at all.My program is concerned with taking a forward-slash-separated URI path as input (example given in question), and producing a backslash-separated relative file system path with the same path segments as output (example given in question). This relative output path could, if desired, then be combined with a base path on some machine to yield an absolute file path.
acdx
+7  A: 

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

Uri uri = new Uri("file://server/filename.ext");
Console.WriteLine(uri.LocalPath);
ydobonmai
This solution worked for me.
Gareth Farrington
+1: Exactly what I was looking for, thanks.
Binary Worrier
@Binary, @Gareth, I am glad that helped you.
ydobonmai
A: 

I figured out this way to produce a full absolute file system path from a relative or absolute URI and a base path.

With:

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

From a 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;
acdx