现总结了C# 获取当前执行程序路径方法大全,以供以后使用及参考。
当前程序所在的位置为:E:\EE\E\PPNames\Test\bin\Debug
string path = "";
//获取模块的完整路径。
path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
path值为:"E:\EE\E\PPNames\Test\bin\Debug\Test.exe"
//获取和设置当前目录(该进程从中启动的目录)的完全限定目录
path = System.Environment.CurrentDirectory;
path值为:"E:\EE\E\PPNames\Test\bin\Debug"
//获取应用程序的当前工作目录
path = System.IO.Directory.GetCurrentDirectory();
path值为:"E:\EE\E\PPNames\Test\bin\Debug"
//获取程序的基目录
path = System.AppDomain.CurrentDomain.BaseDirectory;
path值为:"E:\EE\E\PPNames\Test\bin\Debug\"
//获取和设置包括该应用程序的目录的名称
path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
path值为:"E:\EE\E\PPNames\Test\bin\Debug\"
//获取启动了应用程序的可执行文件的路径
path = System.Windows.Forms.Application.StartupPath;
path值为:"E:\EE\E\PPNames\Test\bin\Debug"
//获取启动了应用程序的可执行文件的路径及文件名
path = System.Windows.Forms.Application.ExecutablePath;
path值为:"E:\EE\E\PPNames\Test\bin\Debug\Test.exe"
获取路径的方法有很多种,需根据项目开发灵活应用。