zoukankan      html  css  js  c++  java
  • C#取得Web程序和非Web程序的根目录的N种取法

     

    几种方法如下:
    非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 + "../目录名";


     

  • 相关阅读:
    [置顶] 【原创分享】嵌入式linux应用之U-BOOT移植定制篇--20130822
    [置顶] java 连接 mysql 数据库步骤
    [置顶] 【原创】无线LED条屏信息报警项目---2012.05
    用Python正则表达式搜索统计命令行管道中的所有数字
    从SharePoint 2013迁移到SharePoint Online
    SharePoint Framework 构建你的第一个web部件(一)
    SharePoint Framework 配置你的SharePoint客户端web部件开发环境
    SharePoint Framework 配置Office 365开发者租户
    SharePoint Framework 开发工具和库
    SharePoint Framework 概述
  • 原文地址:https://www.cnblogs.com/leadwit/p/1333562.html
Copyright © 2011-2022 走看看