C# 获取 .NET Web 应用程序中的当前目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10951599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:55:07  来源:igfitidea点击:

Getting current directory in .NET web application

c#asp.net.netdirectoryfilepath

提问by Julian Coltea

So I have a web project, and I'm trying to get the root directory of the website using the c# method Directory.GetCurrentDirectory(). I don't want to be using a static path as the file locations will be changing in the future. This method is running in my imageProcess.aspx.cs file, but where I thought it would return:

所以我有一个 web 项目,我正在尝试使用 c# 方法获取网站的根目录Directory.GetCurrentDirectory()。我不想使用静态路径,因为文件位置将来会发生变化。这个方法在我的 imageProcess.aspx.cs 文件中运行,但我认为它会返回:

C:\Users\tcbl\documents\visual studio 2010\Projects\ModelMonitoring\ModelMonitoring\imageProcess.aspx.cs

I'm instead getting:

我反而得到:

C:\Program Files\Common Files\Microsoft Shared\DevServer.0\

Can anyone explain why this is happening and what a possible solution might be? Thanks a lot.

谁能解释为什么会发生这种情况以及可能的解决方案是什么?非常感谢。

采纳答案by SLaks

The current directory is a system-level feature; it returns the directory that the server was launched from. It has nothing to do with the website.

当前目录是系统级功能;它返回启动服务器的目录。它与网站无关。

You want HttpRuntime.AppDomainAppPath.

你要HttpRuntime.AppDomainAppPath

If you're in an HTTP request, you can also call Server.MapPath("~/Whatever").

如果您在 HTTP 请求中,您还可以调用Server.MapPath("~/Whatever").

回答by Harshal Doshi Jain

Use this code:

使用此代码:

 HttpContext.Current.Server.MapPath("~")

Detailed Reference:

详细参考:

Server.MapPathspecifies the relative or virtual path to map to a physical directory.

Server.MapPath指定映射到物理目录的相对或虚拟路径。

  • Server.MapPath(".")returns the current physical directory of the file (e.g. aspx) being executed
  • Server.MapPath("..")returns the parent directory
  • Server.MapPath("~")returns the physical path to the root of the application
  • Server.MapPath("/")returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
  • Server.MapPath(".")返回正在执行的文件(例如 aspx)的当前物理目录
  • Server.MapPath("..")返回父目录
  • Server.MapPath("~")返回应用程序根目录的物理路径
  • Server.MapPath("/")返回域名根的物理路径(不一定与应用的根相同)

An example:

一个例子:

Let's say you pointed a web site application (http://www.example.com/) to

假设您将一个网站应用程序 ( http://www.example.com/) 指向

C:\Inetpub\wwwroot

and installed your shop application (sub web as virtual directory in IIS, marked as application) in

并将您的商店应用程序(子网站作为 IIS 中的虚拟目录,标记为应用程序)安装在

D:\WebApps\shop

For example, if you call Server.MapPathin following request:

例如,如果您调用Server.MapPath以下请求:

http://www.example.com/shop/products/GetProduct.aspx?id=2342

then:

然后:

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

If Path starts with either a forward (/) or backward slash (), the MapPathmethod returns a path as if Path were a full, virtual path.

如果 Path 以正斜杠 (/) 或反斜杠 () 开头,则该MapPath方法返回一个路径,就好像 Path 是完整的虚拟路径一样。

If Path doesn't start with a slash, the MapPathmethod returns a path relative to the directory of the request being processed.

如果 Path 不以斜杠开头,则该MapPath方法返回相对于正在处理的请求的目录的路径。

Note: in C#, @ is the verbatim literal string operator meaning that the string should be used "as is" and not be processed for escape sequences.

注意:在 C# 中,@ 是逐字文字字符串运算符,这意味着该字符串应该“按原样”使用,而不是针对转义序列进行处理。

Footnotes

脚注

Server.MapPath(null)and Server.MapPath("")will produce this effect too.

Server.MapPath(null)并且Server.MapPath("")也会产生这种效果。