C#代码获取应用程序物理路径的方法分享


摘要:
下文讲述c# winform代码中,获取其运行路径的方法分享,如下所示:

1.获取应用程序dll所在目录

public static string pathDll = Assembly.GetExecutingAssembly().Location;
//输出结果 X:\xxx\xxx.dll
2.获取exe文件的路径
string exePath= this.GetType().Assembly.Location;
//result: X:\xxx\xxx.exe
3.根据进程名获取完整路径
string procPth = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//result: X:\xxx\xxx.exe
4.获取程序工作目录
string workPath = System.Environment.CurrentDirectory;
//result: X:\xxx\xxx
5.获取进程目录
string processPath = System.AppDomain.CurrentDomain.BaseDirectory;
//result: X:\xxx\xxx\
6. 获取应用程序目录名
string AppPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//result: X:\xxx\xxx\
7.获取应用程序启动路径
string startPath = System.Windows.Forms.Application.StartupPath;
//result: X:\xxx\xxx

8.获取应用程序执行目录
string execPath = System.Windows.Forms.Application.ExecutablePath;
//result: X:\xxx\xxx.exe