C#獲取當前路徑的方法如下:
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -獲取模塊的完整路徑。
2. System.Environment.CurrentDirectory
-獲取和設置當前目錄(該進程從中啓動的目錄)的完全限定目錄。
3. System.IO.Directory.GetCurrentDirectory()
-獲取應用程序的當前工作目錄。這個不一定是程序從中啓動的目錄啊,有可能程序放在C:\www裏,這個函數有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有時不一定返回什麼東東,我也搞不懂了。
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase -獲取和設置包括該應用程序的目錄的名稱。
6. System.Windows.Forms.Application.StartupPath
-獲取啓動了應用程序的可執行文件的路徑。效果和2、5一樣。只是5返回的字符串後面多了一個"\"而已
7. System.Windows.Forms.Application.ExecutablePath
-獲取啓動了應用程序的可執行文件的路徑及文件名,效果和1一樣。在調試時有點區別
我們來看一下效果:
Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
F:\myGit\RotateTransformDemo\Pet\bin\Release\Pet.vshost.exe
Console.WriteLine(System.Environment.CurrentDirectory);
F:\myGit\RotateTransformDemo\Pet\bin\Release
Console.WriteLine(System.IO.Directory.GetCurrentDirectory());
F:\myGit\RotateTransformDemo\Pet\bin\Release
Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
F:\myGit\RotateTransformDemo\Pet\bin\Release\
Console.WriteLine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
F:\myGit\RotateTransformDemo\Pet\bin\Release\
Console.WriteLine(System.Windows.Forms.Application.StartupPath);
F:\myGit\RotateTransformDemo\Pet\bin\Release
Console.WriteLine(System.Windows.Forms.Application.ExecutablePath);
F:\myGit\RotateTransformDemo\Pet\bin\Release\Pet.EXE