获取当前程序所在的目录
// 获取当前程序的执行目录信息
Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory); // asp.net使用,不过在console下也是 /xxx/bin/Debug/net8.0/
Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().Location); // => /xxx/bin/Debug/net8.0/App.dll
Console.WriteLine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));// => /xxx/bin/Debug/net8.0/
Console.WriteLine(Environment.CurrentDirectory); // 取决于当前执行dotnet run命令的路径
// 获取当前程序的执行目录的父级目录
string currentDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string parentDirectory = Directory.GetParent(currentDirectory)?.FullName;
string grandparentDirectory = Directory.GetParent(parentDirectory)?.FullName;
Console.WriteLine("Current Directory: " + currentDirectory);
Console.WriteLine("Parent Directory: " + parentDirectory);
Console.WriteLine("Grandparent Directory: " + grandparentDirectory);
标签:Console,GetExecutingAssembly,System,获取,WriteLine,csharp,Directory,目录
From: https://www.cnblogs.com/zhuoss/p/18139499