zoukankan      html  css  js  c++  java
  • C# 获取程序运行时路径

    Ø  前言

    开发中,很多时候都需要获取程序运行时路径,比如:反射、文件操作等。.NET Framework 已经封装了这些功能,可以很方便的使用。

    C# 中有很多类都可以获取程序运行时路径,我们没必要记住所有的,只需要记住常用的(其他了解即可),比如:

    1.   System.AppDomain.CurrentDomain.BaseDirectory获取基目录,它由程序集冲突解决程序用来探测程序集。

    2.   System.Environment.CurrentDirectory获取或设置当前工作目录的完全限定路径。

    3.   System.IO.Directory.GetCurrentDirectory()获取应用程序的当前工作目录。

    4.   System.Web.HttpRuntime.BinDirectory获取当前应用程序的 /bin 目录的物理路径。

    5.   System.Windows.Forms.Application.StartupPath获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

    1.   所涉及的所有类

    1)   System.AppDomain,程序集:mscorlib.dll

    2)   System.Environment,程序集:mscorlib.dll

    3)   System.IO.Directory,程序集:mscorlib.dll

    4)   System.Reflection.Assembly,程序集:mscorlib.dll

    5)   System.Diagnostics.Process,程序集:System.dll

    6)   System.Web.HttpRuntime,程序集:System.Web.dll

    7)   System.Web.HttpContext,程序集:System.Web.dll

    8)   System.Web.Hosting.HostingEnvironment,程序集:System.Web.dll

    9)   System.Windows.Forms.Application,程序集:System.Windows.Forms.dll

    2.   适用项目

    1)   类库

    2)   Web 应用程序

    3)   ASP.NET MVC

    4)   ASP.NET Web API

    5)   控制台应用程序

    6)   窗体应用程序

     

    1.   System.AppDomain(程序集:mscorlib.dll

    适用项目:通用(不适合 Web API?)。

    1)   CurrentDomain.BaseDirectory获取基目录,它由程序集冲突解决程序用来探测程序集。

    string path = AppDomain.CurrentDomain.BaseDirectory;    //F:ConsoleApplicationinDebug

    或者(两者使用的同一个 System.AppDomain 对象实例)

    string path = System.Threading.Thread.GetDomain().BaseDirectory;    //F:ConsoleApplicationinDebug

     

    2)   CurrentDomain.SetupInformation.ApplicationBase,获取或设置包含该应用程序的目录的名称。

    string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //F:ConsoleApplicationinDebug

     

    2.   System.Environment(程序集:mscorlib.dll

    适用项目:通用(不适合 Web API?)。

    1)   CurrentDirectory获取或设置当前工作目录的完全限定路径。

    string path = Environment.CurrentDirectory; //F:ConsoleApplicationinDebug

     

    2)   GetEnvironmentVariable()从当前进程检索环境变量的值。

    string path = Environment.GetEnvironmentVariable("TEMP");   // C:UsersGOOAppDataLocalTemp

    1.   参数 variable:环境变量名。可选值:

    WINDIRC:WINDOWS

    INCLUDEnull

    TMP/TEMPC:UsersGOOAppDataLocalTemp

    PATH:环境变量路径(很多)

    2.   具体参数值可参考系统环境变量列表,如图:

    clip_image002[6]

     

    3.   System.IO.Directory(程序集:mscorlib.dll

    适用项目:通用(不适合 Web API?)。

    1)   GetCurrentDirectory()获取应用程序的当前工作目录。

    string path = Directory.GetCurrentDirectory();  //F:ConsoleApplicationinDebug

     

    4.   System.Assembly(程序集:mscorlib.dll

    适用项目:通用(不适合 Web API?)。

    1)   GetExecutingAssembly().Location获取包含清单的已加载文件的路径或 UNC 位置。

    string path = Assembly.GetExecutingAssembly().Location; //F:ConsoleApplicationinDebugConsoleApplication.exe

     

    5.   System.Diagnostics.Process(程序集:System.dll

    适用项目:通用(不适合 Web API?)。

    1)   GetCurrentProcess().MainModule.FileName获取模块的完整路径。

    string path = Process.GetCurrentProcess().MainModule.FileName;  //F:ConsoleApplicationinDebugConsoleApplication.vshost.exe

     

    6.   System.Web.HttpRuntime(程序集:System.Web.dll

    适用项目:Web 应用程序、ASP.NET MVCASP.NET Web API

    1)   AppDomainAppPath获取承载在当前应用程序域中的应用程序的应用程序目录的物理驱动器路径。

    string path = HttpRuntime.AppDomainAppPath; //F:WebForm.Basis

     

    2)   BinDirectory获取当前应用程序的 /bin 目录的物理路径。

    string path = HttpRuntime.BinDirectory; //F:WebForm.Basisin

     

    7.   System.Web.HttpContext(程序集:System.Web.dll

    适用项目:Web 应用程序、ASP.NET MVCASP.NET Web API

    1)   Current.Server.MapPath()将指定的虚拟路径映射到物理路径。

    string path = HttpContext.Current.Server.MapPath(@""); //F:WebForm.Basis

    或者

    1.   Web 应用程序(两者使用的同一个 System.Web.HttpServerUtility 对象实例)

    string path = base.Server.MapPath(@"");

    2.   ASP.NET MVC(使用的 System.Web.HttpServerUtilityBase 对象

    string path = base.Server.MapPath(@"");

     

    2)   Current.Request.MapPath()将指定的虚拟路径映射到物理路径。

    string path = HttpContext.Current.Request.MapPath(@"");    //F:WebForm.Basis

    或者

    1.   Web 应用程序(两者使用的同一个 System.Web.HttpRequest 对象实例)

    string path = base.Request.MapPath(@"");

    2.   ASP.NET MVC(使用的 System.Web.HttpRequestBase 对象

    string path = base.Request.MapPath(@"");

     

    3)   Current.Request.PhysicalPath获取与请求的 URL 相对应的物理文件系统路径。

    string path = HttpContext.Current.Request.PhysicalPath; //F:WebForm.BasisRuntimePathTest

    其他项目

    1.   ASP.NET MVC 结果:F:MVC5.BasisBasicOne

    2.   ASP.NET Web API 结果:F:WebAPI2.BasicapiBasicOne

     

    4)   Current.Request.PhysicalApplicationPath获取当前正在执行的服务器应用程序的根目录的物理文件系统路径。

    string path = HttpContext.Current.Request.PhysicalApplicationPath;  //F:WebForm.Basis

    其他项目同上。

     

    5)   Request.Url.AbsoluteUri获取绝对 URI

    string path = HttpContext.Current.Request.Url.AbsoluteUri;  //http://localhost:50049/RuntimePathTest

    其他项目

    1.   ASP.NET MVC 结果:http://localhost:23025/Basic/One

    2.   ASP.NET Web API 结果:http://localhost:48987/api/Basic/One

     

    8.   System.Web.Hosting.HostingEnvironment(程序集:System.Web.dll

    适用项目:Web 应用程序、ASP.NET MVCASP.NET Web API

    1)   ApplicationPhysicalPath获取磁盘上指向应用程序目录的物理路径。

    string path = HostingEnvironment.ApplicationPhysicalPath;   //F:WebForm.Basis

     

    9.   System.Windows.Forms.Application程序集:System.Windows.Forms.dll

    适用项目:窗体应用程序。

    1)   StartupPath获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

    string path = Application.StartupPath;  //F:WinForm.BasicinDebug

     

    2)   ExecutablePath获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

    string path = Application.ExecutablePath;   //F:WinForm.BasicinDebugWinForm.Basic.EXE

  • 相关阅读:
    测试的对自己的要求要高,你自己对测试质量的要求太低了,测试这里放松一下,问题就会放大几倍!!!!测试是最后的质量防线了
    第二阶段:Linux和Bash脚本课程---第五讲:Bash脚本编写
    第二阶段:Linux和Bash脚本课程---第四讲:Bash编程语法
    第二阶段:Linux和Bash脚本课程---第三讲:Linux三剑客与管道使用
    第二阶段:Linux和Bash脚本课程---第二讲:Linux常用命令 (文件/网络/性能)
    selenium 设置cookie,Proxy代理
    request + beautifulsoup + openpyxl + 使用
    给cc爬取一下百度的榜单
    mac电脑-python虚拟环境的维护
    记录一次现网问题定位-5月12号
  • 原文地址:https://www.cnblogs.com/abeam/p/7278833.html
Copyright © 2011-2022 走看看