Reputation: 8430
I have a page something.aspx, with associated codebehind something.aspx.cs. In that codebehind, I want to know the filesystem location of something.aspx. Is there any convenient way to get it?
Update: I got several excellent answers, which unfortunately didn't work because of something else crazy I'm doing. I'm encoding some additional information on the URL I pass in, so it looks like this:
http://server/path/something.aspx/info1/info2/info3.xml
The server deals with this OK (and I'm not using querystring parameters to work around some other code that I didn't write). But when I call Server.MapPath(Request.Url.ToString()) I get an error that the full URL with the 'info' segments isn't a valid virtual path.
Upvotes: 6
Views: 18803
Reputation: 99957
Server.MapPath( Request.AppRelativeCurrentExecutionFilePath )
Upvotes: 0
Reputation: 8736
// File path
string absoluteSystemPath = Server.MapPath("~/relative/path.aspx");
// Directory path
string dir = System.IO.Path.GetDirectoryName(absoluteSystemPath);
// Or simply
string dir2 = Server.MapPath("~/relative");
Upvotes: 10
Reputation: 13699
Server.MapPath is among the most used way to do it.
string physicalPath = Server.MapPath(Request.Url);
Upvotes: 5