XIKEW.COM - 公读宝典 - C# 获取当前路径7种方法 - 公读宝典, - 记录一下网络上找到的,方便日后查找

C# 获取当前路径7种方法
C# CSHARP 3/30/2022 11:27:26 AM 阅读:13

[[toc]]

记录一下网络上找到的,方便日后查找

转载

原文地址

获取当前路径

模块的完整路径

string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

当前目录

该进程从中启动的目录的完全限定目录

string path1 = System.Environment.CurrentDirectory;

应用的当前工作目录

string path1 = System.IO.Directory.GetCurrentDirectory();

程序的基目录

string path1 = System.AppDomain.CurrentDomain.BaseDirectory;

包括该应用的目录名称

string path1 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

启动应用的可执行文件的路径

string path1 = System.Windows.Forms.Application.StartupPath;

启动应用的可执行文件的路径及文件名

string path1 = System.Windows.Forms.Application.ExecutablePath;

例子

示例代码

StringBuilder str = new StringBuilder();
str.AppendLine("System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:" + path1);
str.AppendLine("System.Environment.CurrentDirectory:" + path2);
str.AppendLine("System.IO.Directory.GetCurrentDirectory():" + path3);
str.AppendLine("System.AppDomain.CurrentDomain.BaseDirectory:" + path4);
str.AppendLine("System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:" + path5);
str.AppendLine("System.Windows.Forms.Application.StartupPath:" + path6);
str.AppendLine("System.Windows.Forms.Application.ExecutablePath:" + path7);
string allPath = str.ToString();

输出结果

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.vshost.exe
System.Environment.CurrentDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.IO.Directory.GetCurrentDirectory():D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.AppDomain.CurrentDomain.BaseDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
System.Windows.Forms.Application.StartupPath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.Windows.Forms.Application.ExecutablePath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.EXE