2008-10-09 58 views
6

我有一个页面something.aspx,与相关的代码隐藏something.aspx.cs。在该代码隐藏中,我想知道something.aspx的文件系统位置。有没有方便的方法来获取它?.ASPX页面如何获取其文件系统路径?

更新:我得到了几个很好的答案,很遗憾,由于我正在做其他事情而不工作。我上的编码我通过在URL中一些额外的信息,所以它看起来是这样的:

http://server/path/something.aspx/info1/info2/info3.xml

这个OK的服务器处理(和我没有使用查询字符串参数来解决一些其他的代码我没有写)。但是当我调用Server.MapPath(Request.Url.ToString())时,我得到一个错误,即带有'info'段的完整URL不是有效的虚拟路径。

+0

坐落在MapPath的行设置一个断点,然后检查Request.Url的属性。你会在那里找到很多有用的东西。 – 2008-10-09 17:34:32

+0

会这样,谢谢。 – Bruce 2008-10-09 17:42:05

回答

10
// 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"); 
+0

这意味着我知道'相对'部分是什么,但这就是我想要计算的。 – Bruce 2008-10-09 17:31:27

5

Server.MapPath是最常用的方式去做之一。

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

Server.MapPath(Request.AppRelativeCurrentExecutionFilePath)

相关问题