C# 获取项目路径
最近在进行软件开发,需要获取项目路径,特将C#获取项目路径的方法整理如下。
private void button1_Click(object sender, EventArgs e)
{
// 程序的基目录
//D:\Blog\C#\获取当前路径\Demo01-获取当前路径\bin\Debug\
textBox1.Text = System.AppDomain.CurrentDomain.BaseDirectory;
//模块的完整路径
//D:\Blog\C#\获取当前路径\Demo01-获取当前路径\bin\Debug\Demo01-获取当前路径.exe
textBox2.Text= System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
获取和设置当前目录(该进程从中启动的目录)的完全限定目录
//D:\Blog\C#\获取当前路径\Demo01-获取当前路径\bin\Debug
textBox3.Text = System.Environment.CurrentDirectory;
//获取应用程序的当前工作目录
//D:\Blog\C#\获取当前路径\Demo01-获取当前路径\bin\Debug
textBox4.Text = System.IO.Directory.GetCurrentDirectory();
//获取和设置包括该应用程序的目录的名称
//D:\Blog\C#\获取当前路径\Demo01-获取当前路径\bin\Debug\
textBox5.Text = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//获取启动了应用程序的可执行文件的路径
//D:\Blog\C#\获取当前路径\Demo01-获取当前路径\bin\Debug
textBox6.Text = System.Windows.Forms.Application.StartupPath;
//获取启动了应用程序的可执行文件的路径及文件名
//D:\Blog\C#/获取当前路径/Demo01-获取当前路径/bin/Debug/Demo01-获取当前路径.exe
textBox7.Text = System.Windows.Forms.Application.ExecutablePath;
}
源代码:https://gitcode.com/printer1/BulletFire/tree/master?init=initRepo