Ø 前言
開發中,很多時候都需要擷取程式運作時路徑,比如:反射、檔案操作等。.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)
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. 具體參數值可參考系統環境變量清單,如圖:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicGcq5SM2QDNyQDMxATMtgTNwUDNxQzMyUDM0ADOxAjMtAjM5QTN28CX0ADOxAjMvwFMykDN1YzLcd2bsJ2Lc12bj5ycn9Gbi52YugTMwIzcldWYtl2Lc9CX6MHc0RHaiojIsJye.jpg)
3. System.IO.Directory(程式集:mscorlib.dll)
1) GetCurrentDirectory(),擷取應用程式的目前工作目錄。
string path = Directory.GetCurrentDirectory(); //F:\ConsoleApplication\bin\Debug
4. System.Assembly(程式集:mscorlib.dll)
1) GetExecutingAssembly().Location,擷取包含清單的已加載檔案的路徑或 UNC 位置。
string path = Assembly.GetExecutingAssembly().Location; //F:\ConsoleApplication\bin\Debug\ConsoleApplication.exe
5. System.Diagnostics.Process(程式集:System.dll)
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)
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 對象)
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 對象)
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)
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