在WCF中想使用Server.Map获取当前服务发布目录的绝对路径。可是在调试时却发现:

“System.Web.HttpContext.Current.Server.MapPath("..." + filename);”

“当没有开启ASP.NET相容模式时(aspNetCompatibilityEnabled="true"),WCF不支持HttpContext, HttpApplication,大部分文章都建议修改<serviceHostingEnvironment aspNetCompatibilityEnabled="true">使WCF可使用ASP.NET传统做法。”

最后有两种解决方法:

string t1 = System.Web.HttpContext.Current.Server.MapPath("~");
//以上写法在WCF未开aspNetCompatibilityEnabled时,
//会因HttpContext.Current == null发生错误       
string t2 = System.Web.Hosting.HostingEnvironment.MapPath("~");
//以上写法在WCF中使用没有问题

1. Application.StartupPath——获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。   
2. Environment.CurrentDirectory——获取和设置当前目录(即该进程从中启动的目录)的完全限定路径,只是“当前”目录,不一定是exe文件所在的目录。在用FileOpenDialog选择一个文件以后这个目录就跟着发生改变了。   
3. Application.ExecutablePath——获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
4。System.AppDomain.CurrentDomain.BaseDirectory——获取基目录,它由程序集冲突解决程序用来探测程序集。(windows服务中使用)

程序员的基础教程:菜鸟程序员
posted on 2012-10-20 15:34  itprobie-菜鸟程序员  阅读(7570)  评论(1编辑  收藏  举报