简体   繁体   English

如何在Web服务中获取当前目录

[英]How do I get the current directory in a web service

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. 我正在使用System.IO.Directory.GetCurrentDirectory()来获取Web服务中的当前目录,但这并没有给我当前目录。 How do I get the current directory in a web service? 如何在Web服务中获取当前目录?

Thanks Stuart 谢谢斯图尔特

In a webservice, you are running in a http context. 在Web服务中,您正在http上下文中运行。 So, 所以,

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

will give you the answer. 会给你答案。

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 在这里查看详细信息

You can use 您可以使用

AppDomain.CurrentDomain.BaseDirectory;

This gives you the root directory of your application. 这为您提供了应用程序的根目录。

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(“〜/”)<-根
HttpContext.Current.Server.MapPath(".") <-- CURRENT DIRECTORY HttpContext.Current.Server.MapPath(“。”)<-当前目录
HttpContext.Current.Server.MapPath("..") <-- PARENT DIRECTORY HttpContext.Current.Server.MapPath(“ ..”)<-父目录

All the above is relative, so you can you any combination to traverse the directory tree. 以上所有都是相对的,因此您可以任意组合遍历目录树。

Best way is using 最好的方法是使用

HostingEnvironment.ApplicationPhysicalPath under System.Web.Hosting System.Web.Hosting下的HostingEnvironment.ApplicationPhysicalPath

for more information please refer this link 有关更多信息,请参考此链接

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/") 

HttpContext.Current.Server.MapPath(“ ..”)[观察两个(..)点而不是(。)]给出站点的虚拟目录的物理目录!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM