C# 如何获取 Web 服务中的当前目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/236166/
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
How do I get the current directory in a web service
提问by user23149
I am using System.IO.Directory.GetCurrentDirectory() to get the current directory in my web service, but that does not give me the current directory. How do I get the current directory in a web service?
我正在使用 System.IO.Directory.GetCurrentDirectory() 来获取我的 Web 服务中的当前目录,但这并没有给我当前目录。如何获取 Web 服务中的当前目录?
Thanks Stuart
谢谢斯图尔特
回答by driis
In a webservice, you are running in a http context. So,
在 Web 服务中,您在 http 上下文中运行。所以,
HttpContext.Current.Server.MapPath("~/")
will give you the answer.
会给你答案。
回答by dove
HttpContext.Current.Server.MapPath("~/")would get you the root of the application?
HttpContext.Current.Server.MapPath("~/")会让你得到应用程序的根目录吗?
Which is plenty most likely as you probably know the path from there.
这是最有可能的,因为您可能知道从那里开始的路径。
Another option which might be of interest:
另一个可能感兴趣的选项:
HttpContext.Current.Server.MapPath("/Directory/")
This builds from the root of the application no matter what.
无论如何,这都是从应用程序的根构建的。
Without the first slash this will take directory from where you call as the start:
如果没有第一个斜杠,这将从您调用的目录中获取作为开始:
HttpContext.Current.Server.MapPath("Directory/")
回答by felickz
HttpContext.Current.Server.MapPath(".")will give you the current working directory.
HttpContext.Current.Server.MapPath(".")会给你当前的工作目录。
But to Rohan West's comment about potentially being outside of an HttpContext it would probably be better to just call:
但是对于 Rohan West 关于可能在 HttpContext 之外的评论,最好只调用:
HostingEnvironment.MapPath(".")
See details here
在此处查看详细信息
回答by MiloTheGreat
HttpContext.Current.Server.MapPath("~/") maps back to the root of the application or virtual directory.
HttpContext.Current.Server.MapPath("~/") 映射回应用程序或虚拟目录的根目录。
HttpContext.Current.Server.MapPath("~/") <-- ROOT
HttpContext.Current.Server.MapPath(".") <-- CURRENT DIRECTORY
HttpContext.Current.Server.MapPath("..") <-- PARENT DIRECTORY
HttpContext.Current.Server.MapPath("~/") <-- ROOT
HttpContext.Current.Server.MapPath(".") <-- 当前目录
HttpContext.Current.Server.MapPath("..") <--父目录
All the above is relative, so you can you any combination to traverse the directory tree.
以上都是相对的,所以你可以任意组合遍历目录树。
回答by Damith
回答by HydPhani
HttpContext.Current.Server.MapPath("..") [observe two(..) dots instead of (.)] gives physical directory of Virtual Directory of the site!
HttpContext.Current.Server.MapPath("..") [观察两个(..) 点而不是 (.)] 给出站点虚拟目录的物理目录!
回答by user3866085
You can use
您可以使用
AppDomain.CurrentDomain.BaseDirectory;
This gives you the root directory of your application.
这为您提供了应用程序的根目录。

