在线帮助 ICP备案 合作机构 我要订制
 
 7×24客服热线:13663712063
您的位置:首页  技术支持 C#取得Web程序和非Web程序的根目录的N种取法
       越辰家政管理软件
       越辰空调售后管理软件
       越辰图书销售管理软件
       越辰物业管理软件
       越辰茶楼管理软件
       越辰幼儿园管理软件
       越辰餐饮管理软件
       越辰病历管理系统
       越辰洗浴管理软件
       越辰住院管理系统
       越辰超市管理系统
  

C#取得Web程序和非Web程序的根目录的N种取法

来源:yocin  时间:2010-12-10 20:42:48  栏目:技术支持  点击率:1841

(越辰家政软件部整理)几种方法如下:
非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 + "../目录名";

Copyright ◎2008-2018郑州越辰科技有限公司 版权所有 网站地图
地址:中国·河南郑州市文化路交北环路北晨e时代B座505室 
电话:0371-63399049 13663712063 备案号:豫ICP备19031456号
我们专注于 软件开发| 网站制作  返回欢迎页 
  
点击这里给我发消息 越辰业务
点击这里给我发消息 越辰业务
点击这里给我发消息 越辰代理
点击这里给我发消息 越辰技术
 ,页面执行时间:15.625毫秒        
up close

返回顶部

越辰科技微信
微信扫一扫