C#获取Web和非Web程序的目录


http://blog.csdn.net/hougelou/article/details/8153930


几种方法如下:
非Web程序

1.AppDomain.CurrentDomain.BaseDirectory

2.Environment.CurrentDirectory

3.HttpRuntime.BinDirectory

The path to the current application's/bin directory.


Web程序

HttpCurrent.Context.Server.Mappath();

---------------------------------------------------------------

HttpContext.Current

返回当前请求的 HttpContext 对象。如此我们就可以直接访问Request、Response、Session、Application等对象,和Page中访问等同。
我们无需再将Page用参数的方式传递到我们的类库对象中。

HttpContext.Current.Session["name"] = "猪八戒";
string name = HttpContext.Current.Request.Param["name"];
HttpContext.Current.Response.Write("猪八戒好吃懒做!");


HttpRuntime

为当前应用程序提供一组 ASP.NET 运行时服务。我们可以通过这个类获得当前ASP.NET工程的一些信息。

HttpRuntime.AppDomainAppVirtualPath : 项目虚拟路径
HttpRuntime.AppDomainAppPath : 项目物理路径
HttpRuntime.BinDirectory : BIN目录物理路径
HttpRuntime.ClrInstallDirectory : CLR安装路径(可以用来获取CLR版本号)

Response.Write(string.Format("AppDomainAppId: {0}<br>", HttpRuntime.AppDomainAppId));
Response.Write(string.Format("AppDomainAppPath: {0}<br>", HttpRuntime.AppDomainAppPath));
Response.Write(string.Format("AppDomainAppVirtualPath: {0}<br>", HttpRuntime.AppDomainAppVirtualPath));
Response.Write(string.Format("AppDomainId: {0}<br>", HttpRuntime.AppDomainId));
Response.Write(string.Format("AspInstallDirectory: {0}<br>", HttpRuntime.AspInstallDirectory));
Response.Write(string.Format("BinDirectory: {0}<br>", HttpRuntime.BinDirectory));
Response.Write(string.Format("ClrInstallDirectory: {0}<br>", HttpRuntime.ClrInstallDirectory));
Response.Write(string.Format("CodegenDir: {0}<br>", HttpRuntime.CodegenDir));
Response.Write(string.Format("IsOnUNCShare: {0}<br>", HttpRuntime.IsOnUNCShare.ToString()));
Response.Write(string.Format("MachineConfigurationDirectory: {0}<br>", HttpRuntime.MachineConfigurationDirectory));


输出:
AppDomainAppId: /LM/W3SVC/1/Root/Learn.Test.Web
AppDomainAppPath: D:\System\My Documents\Visual Studio Projects\Learn.Test\Learn.Test.Web\
AppDomainAppVirtualPath: /Learn.Test.Web
AppDomainId: /LM/W3SVC/1/Root/Learn.Test.Web-9-127652564154400560
AspInstallDirectory: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322
BinDirectory: D:\System\My Documents\Visual Studio Projects\Learn.Test\Learn.Test.Web\bin\
ClrInstallDirectory: c:\windows\microsoft.net\framework\v1.1.4322
CodegenDir: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\learn.test.web\41680132\7c880883
IsOnUNCShare: False
MachineConfigurationDirectory: c:\windows\microsoft.net\framework\v1.1.4322\Config

HostingEnvironment

string siteName = HostingEnvironment.SiteName;
string appPath = HostingEnvironment.ApplicationVirtualPath;
string phyPath = HostingEnvironment.ApplicationPhysicalPath;
string adminPath = HostingEnvironment.MapPath("~/Admin");

ApplicationManager.GetApplicationManager().ShutdownApplication(HostingEnvironment.ApplicationID);


灵活运用技巧:

当使用非WEB程序或使用异步调用时,想要取得根目录下的某目录可以使用如下代码:

HttpRuntime.BinDirectory + "../目录名";

获取网站根目录的方法有几种如:

Server.MapPath(Request.ServerVariables["PATH_INFO"])
Server.MapPath("/")
Server.MapPath("")
Server.MapPath(".")
Server.MapPath("../")
Server.MapPath("..") 
Page.Request.ApplicationPath
运行结果:
C:\Inetpub\wwwroot\EnglishClub\manage\WebForm1.aspx
C:\Inetpub\wwwroot\
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\
C:\Inetpub\wwwroot\EnglishClub

以上的方法可以在.aspx中访问,但是如果你在。cs文件就不能用。

HttpContext.Current.Server.MapPath();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath   

在.cs文件中可以用。

但是HttpContext.Current.Server.MapPath();这个获取的是文件的路径而不是根目录。

只有System.Web.HttpContext.Current.Request.PhysicalApplicationPath    这个才是获取的根目录,在写获取数据库路径是应该用这个,其他的都有问题。


测试过,在有些web项目的cs文件中可以这样用。但是最好是用System.Web.HttpContext.Current.Server.MapPath("/SystemManage/Hotel/LocList.xml");

在类库项目中,这个通常是不能用的,这个时候需要引用程序集,引用命名空间System.Web;才行。

System.Environment.CurrentDirectory + @"\IPMS.Web\aboutMe.txt"




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值