C# WinForm获取 当前执行程序路径的几种方法
轉(zhuǎn),原文地址:http://blog.csdn.net/yanlele424/article/details/7329389
1.獲取和設(shè)置當(dāng)前目錄的完全限定路徑。
string str = System.Environment.CurrentDirectory;
Result: C:xxxxxx
2.獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑,不包括可執(zhí)行文件的名稱。
string str = System.Windows.Forms.Application.StartupPath;
Result: C:xxxxxx
3.獲取新的 Process 組件并將其與當(dāng)前活動(dòng)的進(jìn)程關(guān)聯(lián)的主模塊的完整路徑,包含文件名。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
Result: C:xxxxxxxxx.exe
4.獲取當(dāng)前 Thread 的當(dāng)前應(yīng)用程序域的基目錄,它由程序集沖突解決程序用來探測程序集。
string str = System.AppDomain.CurrentDomain.BaseDirectory;
Result: C:xxxxxx
5.獲取應(yīng)用程序的當(dāng)前工作目錄。
string str = System.IO.Directory.GetCurrentDirectory();
Result: C:xxxxxx
6.獲取和設(shè)置包含該應(yīng)用程序的目錄的名稱。
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
Result: C:xxxxxx
7.獲取當(dāng)前進(jìn)程的完整路徑,包含文件名。
string str = this.GetType().Assembly.Location;
Result: C:xxxxxxxxx.exe
8.獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑,包括可執(zhí)行文件的名稱。
string str = System.Windows.Forms.Application.ExecutablePath;
Result: C:xxxxxxxxx.exe
---------------------------------------------------------------------------------
在dll中有時(shí)需要使用主調(diào)用程序中的資源,這就要正確獲取調(diào)用程序的文件名及其路徑等信息。這需要和調(diào)用dll本身的文件名和路徑區(qū)分開來!
?
?? ?這就牽扯到System.Reflection.Assembly程序集類使用了。
?? ??GetExecutingAssembly?:?獲取包含當(dāng)前執(zhí)行的代碼的程序集
?? ??GetCallingAssembly?:?返回調(diào)用當(dāng)前正在執(zhí)行的方法的方法的 System.Reflection.Assembly
?
轉(zhuǎn)載于:https://www.cnblogs.com/wumingpu/p/5257097.html
總結(jié)
以上是生活随笔為你收集整理的C# WinForm获取 当前执行程序路径的几种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python+selenium+Robo
- 下一篇: 李开复:AlphaGo 若打败了世界冠军