Ø 前言
開發中,很多時候都需要獲取程序運行時路徑,比如:反射、文件操作等。.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