C# 獲取exe當前運行目錄的5種方法和區別


          string str1 = Process.GetCurrentProcess().MainModule.FileName;//可獲得當前執行的exe的文件名。
                string str2 = Environment.CurrentDirectory;//獲取和設置當前目錄(即該進程從中啟動的目錄)的完全限定路徑。(備注:按照定義,如果該進程在本地或網絡驅動器的根目錄中啟動,則此屬性的值為驅動器名稱后跟一個尾部反斜杠(如“C:\”)。如果該進程在子目錄中啟動,則此屬性的值為不帶尾部反斜杠的驅動器和子目錄路徑[如“C:\mySubDirectory”])。
                string str3 = Directory.GetCurrentDirectory(); //獲取應用程序的當前工作目錄。
                string str4 = AppDomain.CurrentDomain.BaseDirectory;//獲取基目錄,它由程序集沖突解決程序用來探測程序集。
                string str5 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取或設置包含該應用程序的目錄的名稱。
                WriteLog("str1:" + str1);
                WriteLog("str2:" + str2);
                WriteLog("str3:" + str3);
                WriteLog("str4:" + str4);
                WriteLog("str5:" + str5);

雙擊exe直接打開:

2021/10/20 15:26:55   str1:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\KafkaReceive.exe
2021/10/20 15:26:55   str2:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive
2021/10/20 15:26:55   str3:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive
2021/10/20 15:26:55   str4:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\
2021/10/20 15:26:55   str5:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\

 

在MVC項目中,使用微軟cmd命令模塊的方式打開:

2021/10/20 15:26:01   str1:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\KafkaReceive.exe
2021/10/20 15:26:01   str2:c:\windows\system32\inetsrv
2021/10/20 15:26:01   str3:c:\windows\system32\inetsrv
2021/10/20 15:26:01   str4:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\
2021/10/20 15:26:01   str5:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM