控制台应用程序中的路径
static void Main(string[] args)
{
Console.WriteLine("Environment.CurrentDirectory:" + Environment.CurrentDirectory);
Console.WriteLine("AppDomain.CurrentDomain.BaseDirectory:"+AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine("项目根目录:"+Path.GetFullPath("../../"));
Console.ReadKey();
}
-
Environment.CurrentDirectory:D:\Cpeoject\ConsolePath\bin\Debug
-
AppDomain.CurrentDomain.BaseDirectory:D:\Cpeoject\ConsolePath\bin\Debug\
-
项目根目录:D:\Cpeoject\ConsolePath\
Form中的路径
Dictionary<string, string> formPathDic = new Dictionary<string, string>();
string path1 = Environment.CurrentDirectory;
formPathDic.Add("Environment.CurrentDirectory", path1);
string path2 = Application.StartupPath;
formPathDic.Add("Application.StartupPath", path2);
string path3 = Directory.GetCurrentDirectory();
formPathDic.Add("Directory.GetCurrentDirectory()", path3);
string path4 = AppDomain.CurrentDomain.BaseDirectory;
formPathDic.Add("AppDomain.CurrentDomain.BaseDirectory", path4);
string path6 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
formPathDic.Add("SetupInformation.ApplicationBase",path6);
string path7 = Process.GetCurrentProcess().MainModule.FileName;
formPathDic.Add("MainModule.FileName",path7);
string path8 = Application.ExecutablePath;
formPathDic.Add("Application.ExecutablePath", path8);
DirectoryInfo dire = Directory.GetParent(Directory.GetCurrentDirectory());
string path9 = dire.Parent.FullName;
formPathDic.Add("RootPath",path9);
foreach (KeyValuePair<string, string> kvp in formPathDic)
{
listBox1.Items.Add(kvp.Key);
listBox2.Items.Add(kvp.Value);
}