代码
//1.获取模块的完整路径。
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//2.获取和设置当前目录(该进程从中启动的目录)的完全限定目录
string path2 = System.Environment.CurrentDirectory;
//3.获取应用程序的当前工作目录
string path3 = System.IO.Directory.GetCurrentDirectory();
//4.获取程序的基目录
string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
//5.获取和设置包括该应用程序的目录的名称
string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//6.获取启动了应用程序的可执行文件的路径
string path6 = System.Windows.Forms.Application.StartupPath;
//7.获取启动了应用程序的可执行文件的路径及文件名
string path7 = System.Windows.Forms.Application.ExecutablePath;
执行结果:
获取模块的完整路径:E:\Catullus\CatullusNextVPU\AnalysisEndFile_CfgFile\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\WindowsFormsApp1.exe
获取和设置当前目录(该进程从中启动的目录)的完全限定目录:E:\Catullus\CatullusNextVPU\AnalysisEndFile_CfgFile\WindowsFormsApp1\WindowsFormsApp1\bin\Debug
获取应用程序的当前工作目录:E:\Catullus\CatullusNextVPU\AnalysisEndFile_CfgFile\WindowsFormsApp1\WindowsFormsApp1\bin\Debug
获取程序的基目录:E:\Catullus\CatullusNextVPU\AnalysisEndFile_CfgFile\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\
获取和设置包括该应用程序的目录的名称:E:\Catullus\CatullusNextVPU\AnalysisEndFile_CfgFile\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\
获取启动了应用程序的可执行文件的路径:E:\Catullus\CatullusNextVPU\AnalysisEndFile_CfgFile\WindowsFormsApp1\WindowsFormsApp1\bin\Debug
获取启动了应用程序的可执行文件的路径及文件名:E:\Catullus\CatullusNextVPU\AnalysisEndFile_CfgFile\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\WindowsFormsApp1.exe
使用推荐
如果想获取可执行文件的所在目录的路径,用:
string path6 = System.Windows.Forms.Application.StartupPath;
当前工作目录 和 可执行文件的路径 的区别
System.IO.Directory.GetCurrentDirectory()方法用于获得应用程序当前工作目录。
System.Windows.Forms.Application.StartupPath 获得应用程序的可执行文件的路径,不含文件名
StartupPath 可执行文件固定就不会变,可用于定位
GetCurrentDirectory 不一定是可执行文件所在目录,因启动的位置而变
如有两个程序 C:/A/a.exe 和 C:/B/b.exe
当在 a.exe 中启动 b.exe,System.Diagnostics.Process.Start(@“C:/B/b.exe”)
这时,b.exe 中 Directory.GetCurrentDirectory()方法 返回值为“C:/A”,Application.StartupPath 值为“C:/B”
如果直接在 C:/B/ 中启动 b.exe,那么两个值就会一样,都为“C:/B”
这就是 Directory.GetCurrentDirectory 和 Application.StartupPath 的区别
两个意义是不一样的.
Application.StartupPath是程序的启动目录,这个在程序运行以后,就不会改变了.
Directory.GetCurrentDirectory()是 “当前目录 “,是可以在程序运行时候改变的,用
Directory.SetCurrentDirectory()就可以改变.
Directory.GetCurrentDirectory()的缺省值是和Application.StartupPath()一样的.
参考:
https://blog.csdn.net/wangtao19932008/article/details/108781933
https://blog.csdn.net/u014647208/article/details/86149326