https://www.cnblogs.com/abeam/p/7278833.html
开发中,很多时候都需要获取程序运行时路径,比如:反射、文件操作等。.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:\ConsoleApplication\bin\Debug\ 或者(两者使用的同一个 System.AppDomain 对象实例) string path = System.Threading.Thread.GetDomain().BaseDirectory; //F:\ConsoleApplication\bin\Debug\ 2) CurrentDomain.SetupInformation.ApplicationBase,获取或设置包含该应用程序的目录的名称。 string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //F:\ConsoleApplication\bin\Debug\ 2. System.Environment(程序集:mscorlib.dll) 适用项目:通用(不适合 Web API?)。 1) CurrentDirectory,获取或设置当前工作目录的完全限定路径。 string path = Environment.CurrentDirectory; //F:\ConsoleApplication\bin\Debug 2) GetEnvironmentVariable(),从当前进程检索环境变量的值。 string path = Environment.GetEnvironmentVariable("TEMP"); // C:\Users\GOO\AppData\Local\Temp 1. 参数 variable:环境变量名。可选值: WINDIR:C:\WINDOWS INCLUDE:null TMP/TEMP:C:\Users\GOO\AppData\Local\Temp PATH:环境变量路径(很多) 2. 具体参数值可参考系统环境变量列表,如图: 3. System.IO.Directory(程序集:mscorlib.dll) 适用项目:通用(不适合 Web API?)。 1) GetCurrentDirectory(),获取应用程序的当前工作目录。 string path = Directory.GetCurrentDirectory(); //F:\ConsoleApplication\bin\Debug 4. System.Assembly(程序集:mscorlib.dll) 适用项目:通用(不适合 Web API?)。 1) GetExecutingAssembly().Location,获取包含清单的已加载文件的路径或 UNC 位置。 string path = Assembly.GetExecutingAssembly().Location; //F:\ConsoleApplication\bin\Debug\ConsoleApplication.exe 5. System.Diagnostics.Process(程序集:System.dll) 适用项目:通用(不适合 Web API?)。 1) GetCurrentProcess().MainModule.FileName,获取模块的完整路径。 string path = Process.GetCurrentProcess().MainModule.FileName; //F:\ConsoleApplication\bin\Debug\ConsoleApplication.vshost.exe 6. System.Web.HttpRuntime(程序集:System.Web.dll) 适用项目:Web 应用程序、ASP.NET MVC、ASP.NET Web API。 1) AppDomainAppPath,获取承载在当前应用程序域中的应用程序的应用程序目录的物理驱动器路径。 string path = HttpRuntime.AppDomainAppPath; //F:\WebForm.Basis\ 2) BinDirectory,获取当前应用程序的 /bin 目录的物理路径。 string path = HttpRuntime.BinDirectory; //F:\WebForm.Basis\bin\ 7. System.Web.HttpContext(程序集:System.Web.dll) 适用项目:Web 应用程序、ASP.NET MVC、ASP.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.Basis\RuntimePathTest 其他项目 1. ASP.NET MVC 结果:F:\MVC5.Basis\Basic\One 2. ASP.NET Web API 结果:F:\WebAPI2.Basic\api\Basic\One 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 MVC、ASP.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.Basic\bin\Debug 2) ExecutablePath:获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。 string path = Application.ExecutablePath; //F:\WinForm.Basic\bin\Debug\WinForm.Basic.EXE
|