1. 程式人生 > WINDOWS開發 >C# 獲取當前路徑 (exe、dll)

C# 獲取當前路徑 (exe、dll)

//獲取模組的完整路徑。
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//獲取和設定當前目錄(該程序從中啟動的目錄)的完全限定目錄
string path2 = System.Environment.CurrentDirectory;
//獲取應用程式的當前工作目錄
string path3 = System.IO.Directory.GetCurrentDirectory();
//獲取程式的基目錄
string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
//獲取和設定包括該應用程式的目錄的名稱 string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //獲取啟動了應用程式的可執行檔案的路徑 string path6 = System.Windows.Forms.Application.StartupPath; //獲取啟動了應用程式的可執行檔案的路徑及檔名 string path7 = System.Windows.Forms.Application.ExecutablePath;

string path8 = System.Reflection.Assembly.GetExecutingAssembly().Location;

string path9 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

前7種獲取的都是exe所在目錄,後兩種獲取的是dll所在目錄

技術分享圖片

public static string AssemblyDirectory
{
    get
    {
        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
        UriBuilder uri = new
UriBuilder(codeBase); string path = Uri.UnescapeDataString(uri.Path); return Path.GetDirectoryName(path); } }