Jul*_*tea 101 .net c# asp.net directory filepath
所以我有一个Web项目,我正在尝试使用c#方法获取网站的根目录Directory.GetCurrentDirectory()
.我不想使用静态路径,因为文件位置将来会发生变化.这个方法在我的imageProcess.aspx.cs文件中运行,但我认为它会返回:
C:\Users\tcbl\documents\visual studio 2010\Projects\ModelMonitoring\ModelMonitoring\imageProcess.aspx.cs
Run Code Online (Sandbox Code Playgroud)
我反而得到:
C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么会发生这种情况以及可能的解决方案是什么?非常感谢.
SLa*_*aks 204
当前目录是系统级功能; 它返回启动服务器的目录.它与网站无关.
你想要的HttpRuntime.AppDomainAppPath
.
如果您在HTTP请求中,也可以打电话Server.MapPath("~/Whatever")
.
Har*_*ain 102
使用此代码:
HttpContext.Current.Server.MapPath("~")
Run Code Online (Sandbox Code Playgroud)
详细参考:
Server.MapPath
指定映射到物理目录的相对路径或虚拟路径.
Server.MapPath(".")
返回正在执行的文件(例如aspx)的当前物理目录Server.MapPath("..")
返回父目录Server.MapPath("~")
返回应用程序根目录的物理路径Server.MapPath("/")
返回域名根目录的物理路径(不一定与应用程序的根目录相同)一个例子:
假设你指的是一个网站应用程序(http://www.example.com/)
C:\Inetpub\wwwroot
Run Code Online (Sandbox Code Playgroud)
并安装了您的商店应用程序(子网站为IIS中的虚拟目录,标记为应用程序)
D:\WebApps\shop
Run Code Online (Sandbox Code Playgroud)
例如,如果您致电Server.MapPath
以下请求:
http://www.example.com/shop/products/GetProduct.aspx?id=2342
Run Code Online (Sandbox Code Playgroud)
然后:
Server.MapPath(".") returns D:\WebApps\shop\products
Server.MapPath("..") returns D:\WebApps\shop
Server.MapPath("~") returns D:\WebApps\shop
Server.MapPath("/") returns C:\Inetpub\wwwroot
Server.MapPath("/shop") returns D:\WebApps\shop
Run Code Online (Sandbox Code Playgroud)
如果Path以正向(/)或反斜杠()开头,则该MapPath
方法返回路径,就好像Path是完整的虚拟路径一样.
如果Path不以斜杠开头,则该MapPath
方法返回相对于正在处理的请求的目录的路径.
注意:在C#中,@是逐字文字字符串运算符,意味着字符串应该"按原样"使用,而不是为转义序列处理.
脚注
Server.MapPath(null)
并且Server.MapPath("")
也会产生这种效果.
对于点网 6 我使用:
AppContext.BaseDirectory
Run Code Online (Sandbox Code Playgroud)
很酷的一点是,它在 ASP.NET 和控制台应用程序上都是相同的。
归档时间: |
|
查看次数: |
181185 次 |
最近记录: |