c# 獲取當前應用程序的路徑


C# 獲取程序路徑的主要方法和區別如下:

```csharp
//1.獲取模塊的完整路徑(即程序名+.vshost.exe)是visual studio宿主應用程序,vs運行調試時是打開的其實是這個文件,這個程序可以讓vs跟蹤調試信息。
            string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug\SD_MyTest.vshost.exe

            //2.獲取和設置當前目錄(該進程從中啟動的目錄)的完全限定目錄 
            string path2 = System.Environment.CurrentDirectory;
            //C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug

            //3.獲取應用程序的當前工作目錄 
            string path3 = System.IO.Directory.GetCurrentDirectory();
            //C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug

            //4.獲取程序的基目錄 
            string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
            //C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug\

            //5.獲取和設置包括該應用程序的目錄的名稱 
            string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            //C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug\

            //6.獲取啟動了應用程序的可執行文件的路徑 
            string path6 = System.Windows.Forms.Application.StartupPath;
            //C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug

            //7.獲取啟動了應用程序的可執行文件的路徑及文件名 
            string path7 = System.Windows.Forms.Application.ExecutablePath;
            //C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug\SD_MyTest.EXE
```

 


免責聲明!

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



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