Bruce
Bruce

Reputation: 8430

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

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

Answers (4)

Mark Cidade
Mark Cidade

Reputation: 99957

Server.MapPath( Request.AppRelativeCurrentExecutionFilePath )

Upvotes: 0

Jason DeFontes
Jason DeFontes

Reputation: 2285

Request.PhysicalPath

Upvotes: 8

David Thibault
David Thibault

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

Maxime Rouiller
Maxime Rouiller

Reputation: 13699

Server.MapPath is among the most used way to do it.

string physicalPath = Server.MapPath(Request.Url);

Upvotes: 5

Related Questions