C# 获取程序路径的主要方法和区别如下

C# 获取程序路径的主要方法和区别如下:

```csharp
//1.获取模块的完整路径(即程序名+.vshost.exe)是visual studio宿主应用程序,vs运行调试时是打开的其实是这个文件,这个程序可以让vs跟踪调试信息。
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug\SD_MyTest.vshost.exe

//2.获取和设置当前目录(该进程从中启动的目录)的完全限定目录
string path2 = System.Environment.CurrentDirectory;
//C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug

//3.获取应用程序的当前工作目录
string path3 = System.IO.Directory.GetCurrentDirectory();
//C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug

//4.获取程序的基目录
string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
//C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug\

//5.获取和设置包括该应用程序的目录的名称
string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug\

//6.获取启动了应用程序的可执行文件的路径
string path6 = System.Windows.Forms.Application.StartupPath;
//C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug

//7.获取启动了应用程序的可执行文件的路径及文件名
string path7 = System.Windows.Forms.Application.ExecutablePath;
//C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SD_MyTest\bin\Debug\SD_MyTest.EXE
```

猜你喜欢

转载自www.cnblogs.com/liangyuwen/p/13205374.html