C#获取当前项目路径的各种方式(System.AppDomain.*、System.Environment.*、System.IO.*...)
//获取模块的完整路径。 string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; //获取和设置当前目录(该进程从中启动的目录)的完全限定目录 string path2 = System.Environment.CurrentDirectory; //获取应用程序的当前工作目录 string path3 = System.IO.Directory.GetCurrentDirectory(); //获取程序的基目录 string path4 = System.AppDomain.CurrentDomain.BaseDirectory; //获取和设置包括该应用程序的目录的名称 string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //获取启动了应用程序的可执行文件的路径 string path6 = System.Windows.Forms.Application.StartupPath; //获取启动了应用程序的可执行文件的路径及文件名 string path7 = System.Windows.Forms.Application.ExecutablePath; //获取或设置当前程序集的URL位置 string path8 = Assembly.GetExecutingAssembly().GetName().CodeBase; //获取当前程序集包含清单的已加载文件的路径或UNC位置 string path9 = Assembly.GetExecutingAssembly().Location;
// 输出结果:
E:\WORK\测试\ConsoleApplication4\bin\Debug\ConsoleApplication4.vshost.exe //path1
E:\WORK\测试\ConsoleApplication4\bin\Debug //path2
E:\WORK\测试\ConsoleApplication4\bin\Debug //path3
E:\WORK\测试\ConsoleApplication4\bin\Debug\ //path4
E:\WORK\测试\ConsoleApplication4\bin\Debug\ //path5
E:\WORK\测试\ConsoleApplication4\bin\Debug //path6
E:\WORK\测试\ConsoleApplication4\bin\Debug\ConsoleApplication4.EXE //path7
file:///E:/WORK/测试/ConsoleApplication4/bin/Debug/ConsoleApplication4.EXE //path8
E:\WORK\测试\ConsoleApplication4\bin\Debug\ConsoleApplication4.exe //path9