一、獲取完整包含執(zhí)行程序的路徑:exe文件所在的目錄+.exe文件名
1、方法1:Type.Assembly.Location
//獲取當(dāng)前進程的完整路徑,包含文件名(進程名)。
string str = this.GetType().Assembly.Location;
結(jié)果:X:\xxx\xxx\xxx.exe(.exe文件所在的目錄+.exe文件名)
2、方法2:System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
//獲取新的 Process 組件并將其與當(dāng)前活動的進程關(guān)聯(lián)的主模塊的完整路徑,包含文件名(進程名)。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
結(jié)果:X:\xxx\xxx\xxx.exe(.exe文件所在的目錄+.exe文件名)
3、方法3:System.Windows.Forms.Application.ExecutablePath
//獲取啟動了應(yīng)用程序的可執(zhí)行文件的路徑,包括可執(zhí)行文件的名稱。
string str = System.Windows.Forms.Application.ExecutablePath;
結(jié)果:X:\xxx\xxx\xxx.exe(.exe文件所在的目錄+.exe文件名)
二、獲取當(dāng)前程序所在路徑:exe文件所在的目錄(不包含xxx.exe)
1、方法1:System.Environment.CurrentDirectory
//獲取和設(shè)置當(dāng)前目錄(即該進程從中啟動的目錄)的完全限定路徑。
string str = System.Environment.CurrentDirectory;
結(jié)果:X:\xxx\xxx(.exe文件所在的目錄)
2、方法2:System.AppDomain.CurrentDomain.BaseDirectory
//獲取當(dāng)前 Thread 的當(dāng)前應(yīng)用程序域的基目錄,它由程序集沖突解決程序用來探測程序集。
string str = System.AppDomain.CurrentDomain.BaseDirectory;
結(jié)果:X:\xxx\xxx(.exe文件所在的目錄)
3、方法3:System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
//獲取和設(shè)置包含該應(yīng)用程序的目錄的名稱。
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
結(jié)果:X:\xxx\xxx\(.exe文件所在的目錄+“”)
4、方法4:System.Windows.Forms.Application.StartupPath
//獲取啟動了應(yīng)用程序的可執(zhí)行文件的路徑,不包括可執(zhí)行文件的名稱。
string str = System.Windows.Forms.Application.StartupPath;
結(jié)果:X:\xxx\xxx(.exe文件所在的目錄)文章來源:http://www.zghlxwxcb.cn/news/detail-672044.html
5、方法5:System.IO.Directory.GetCurrentDirectory()
//獲取應(yīng)用程序的當(dāng)前工作目錄(不可靠)。
string str = System.IO.Directory.GetCurrentDirectory();
結(jié)果:X:\xxx\xxx(.exe文件所在的目錄)文章來源地址http://www.zghlxwxcb.cn/news/detail-672044.html
到了這里,關(guān)于C#獲取當(dāng)前程序所在路徑的各種方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!