Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can an .ASPX page get its file system path?

Tags:

asp.net

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.

like image 964
Bruce Avatar asked Oct 09 '08 17:10

Bruce


1 Answers

// 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");
like image 76
David Thibault Avatar answered Oct 11 '22 14:10

David Thibault