溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

.NET怎么獲取當(dāng)前路徑

發(fā)布時(shí)間:2020-09-14 15:12:38 來源:億速云 閱讀:253 作者:小新 欄目:編程語言

小編給大家分享一下.NET怎么獲取當(dāng)前路徑,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

以下匯總了.NET(包括ASP.NET/WinForm等)獲取當(dāng)前路徑的各種方法

//獲取當(dāng)前進(jìn)程的完整路徑,包含文件名(進(jìn)程名)。  
string str = this.GetType().Assembly.Location;  
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

//獲取新的 Process 組件并將其與當(dāng)前活動(dòng)的進(jìn)程關(guān)聯(lián)的主模塊的完整路徑,包含文件名(進(jìn)程名)。  
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;  
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

//獲取和設(shè)置當(dāng)前目錄(即該進(jìn)程從中啟動(dòng)的目錄)的完全限定路徑。  
string str = System.Environment.CurrentDirectory;  
result: X:\xxx\xxx (.exe文件所在的目錄)

//獲取當(dāng)前 Thread 的當(dāng)前應(yīng)用程序域的基目錄,它由程序集沖突解決程序用來探測(cè)程序集。  
string str = System.AppDomain.CurrentDomain.BaseDirectory;  
result: X:\xxx\xxx\ (.exe文件所在的目錄+"\")

//獲取和設(shè)置包含該應(yīng)用程序的目錄的名稱。  
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;  
result: X:\xxx\xxx\ (.exe文件所在的目錄+"\")

//獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑,不包括可執(zhí)行文件的名稱。  
string str = System.Windows.Forms.Application.StartupPath;  
result: X:\xxx\xxx (.exe文件所在的目錄)

//獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑,包括可執(zhí)行文件的名稱。  
string str = System.Windows.Forms.Application.ExecutablePath;  
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

//獲取應(yīng)用程序的當(dāng)前工作目錄(不可靠)。  
string str = System.IO.Directory.GetCurrentDirectory();  
result: X:\xxx\xxx (.exe文件所在的目錄)

//獲取當(dāng)前進(jìn)程的完整路徑,包含文件名(進(jìn)程名)。
string str = this.GetType().Assembly.Location;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

//獲取新的 Process 組件并將其與當(dāng)前活動(dòng)的進(jìn)程關(guān)聯(lián)的主模塊的完整路徑,包含文件名(進(jìn)程名)。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

//獲取和設(shè)置當(dāng)前目錄(即該進(jìn)程從中啟動(dòng)的目錄)的完全限定路徑。
string str = System.Environment.CurrentDirectory;
result: X:\xxx\xxx (.exe文件所在的目錄)

//獲取當(dāng)前 Thread 的當(dāng)前應(yīng)用程序域的基目錄,它由程序集沖突解決程序用來探測(cè)程序集。
string str = System.AppDomain.CurrentDomain.BaseDirectory;
result: X:\xxx\xxx\ (.exe文件所在的目錄+"\")

//獲取和設(shè)置包含該應(yīng)用程序的目錄的名稱。
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:\xxx\xxx\ (.exe文件所在的目錄+"\")

//獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑,不包括可執(zhí)行文件的名稱。
string str = System.Windows.Forms.Application.StartupPath;
result: X:\xxx\xxx (.exe文件所在的目錄)

//獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑,包括可執(zhí)行文件的名稱。
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

//獲取應(yīng)用程序的當(dāng)前工作目錄(不可靠)。
string str = System.IO.Directory.GetCurrentDirectory();
result: X:\xxx\xxx (.exe文件所在的目錄)

.NET中三種獲取當(dāng)前路徑的代碼

//Web編程  
HttpContext.Current.Server.MapPath("FileName")  
System.Web.HttpContext.Current.Request.Path

//Windows編程  
System.Environment.CurrentDirectory

//Mobile編程  
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

以上是.NET怎么獲取當(dāng)前路徑的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI