tags:

views:

1871

answers:

4

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.

+6  A: 
// 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");
David Thibault
this implies I know what the 'relative' part is, but that's what I'm trying to calculate.
Bruce
But it works for me - so: +1
BlaM
+4  A: 

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

string physicalPath = Server.MapPath(Request.Url);
Maxim
+4  A: 

Request.PhysicalPath

Jason DeFontes
Winner! The Request object is so poorly organized it's hard to remember all the different places for stuff.
John Sheehan
That did the trick, thanks! The actual line I ended up with is:string servicesPath = Path.GetDirectoryName(Request.PhysicalPath) + @"\services\";
Bruce
A: 

Server.MapPath( Request.AppRelativeCurrentExecutionFilePath )

Mark Cidade