C#中获取进程当前路径各种方法的测试
在CSharp中,获取当前进程的路径有很多种方式。同一个api在不同的运行和发布方式中,又会产生不同的效果。
下面我用代码来测试一下效果,运行环境是:Windows 10,.Net 8。
测试程序为放在``D:\的
CurrentPathTest`目录。
//不同的发布及运行方式
//1. 发布为生成跨平台二进制文件,使用`dotnet CurrentPathTest.dll`启动程序
//2. 发布为独立应用,直接执行exe文件启动程序
//3. 发布为独立应用,安装到windows服务启动
//4. 发布为单文件独立应用`dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true`
//5. 发布为单文件自解压独立应用`dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true /p:IncludeAllContentForSelfExtract=true`
string currentPath = "D:\\CurrentPath.txt";
File.AppendAllText(currentPath, $"\r\n");
string str = this.GetType().Assembly.Location;
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. D:\CurrentPathTest\CurrentPathTest.dll
//2. D:\CurrentPathTest\CurrentPathTest.dll
//3. D:\CurrentPathTest\CurrentPathTest.dll
//4.
//5. C:\Users\admin\AppData\Local\Temp\.net\CurrentPathTest\l5efxJxdB_e7Z5zW2Hw4J3OtGjuVIsY=\CurrentPathTest.dll
str = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName ?? "";
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. C:\Program Files\dotnet\dotnet.exe
//2. D:\CurrentPathTest\CurrentPathTest.exe
//3. D:\CurrentPathTest\CurrentPathTest.exe
//4. D:\CurrentPathTest\CurrentPathTest.exe
//5. D:\CurrentPathTest\CurrentPathTest.exe
str = System.Diagnostics.Process.GetCurrentProcess().MainModule?.ModuleName ?? "";
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. dotnet.exe
//2. CurrentPathTest.exe
//3. CurrentPathTest.exe
//4. CurrentPathTest.exe
//5. CurrentPathTest.exe
str = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. dotnet
//2. CurrentPathTest
//3. CurrentPathTest
//4. CurrentPathTest
//5. CurrentPathTest
str = System.Environment.CurrentDirectory;
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. D:\CurrentPathTest
//2. D:\CurrentPathTest
//3. C:\WINDOWS\system32
//4. D:\CurrentPathTest
//5. D:\CurrentPathTest
str = System.Environment.ProcessPath ?? "";
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. C:\Program Files\dotnet\dotnet.exe
//2. D:\CurrentPathTest\CurrentPathTest.exe
//3. D:\CurrentPathTest\CurrentPathTest.exe
//4. D:\CurrentPathTest\CurrentPathTest.exe
//5. D:\CurrentPathTest\CurrentPathTest.exe
str = System.Environment.GetCommandLineArgs()[0];
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. D:\CurrentPathTest\CurrentPathTest.dll
//2. D:\CurrentPathTest\CurrentPathTest.dll
//3. D:\CurrentPathTest\CurrentPathTest.dll
//4. D:\CurrentPathTest\CurrentPathTest.exe
//5. D:\CurrentPathTest\CurrentPathTest.exe
str = System.AppDomain.CurrentDomain.BaseDirectory;
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. D:\CurrentPathTest\
//2. D:\CurrentPathTest\
//3. D:\CurrentPathTest\
//4. D:\CurrentPathTest\
//5. C:\Users\admin\AppData\Local\Temp\.net\CurrentPathTest\l5efxJxdB_e7Z5zW2Hw4J3OtGjuVIsY=\
str = System.AppDomain.CurrentDomain.FriendlyName;
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. CurrentPathTest
//2. CurrentPathTest
//3. CurrentPathTest
//4. CurrentPathTest
//5. CurrentPathTest
str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase ?? "";
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. D:\CurrentPathTest\
//2. D:\CurrentPathTest\
//3. D:\CurrentPathTest\
//4. D:\CurrentPathTest\
//5. C:\Users\admin\AppData\Local\Temp\.net\CurrentPathTest\l5efxJxdB_e7Z5zW2Hw4J3OtGjuVIsY=\
str = System.IO.Directory.GetCurrentDirectory();
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. D:\CurrentPathTest
//2. D:\CurrentPathTest
//3. C:\WINDOWS\system32
//4. D:\CurrentPathTest
//5. D:\CurrentPathTest
str = System.AppContext.BaseDirectory;
File.AppendAllText(currentPath, $"{str}");
File.AppendAllText(currentPath, $"\r\n");
//1. D:\CurrentPathTest\
//2. D:\CurrentPathTest\
//3. D:\CurrentPathTest\
//4. D:\CurrentPathTest\
//5. C:\Users\admin\AppData\Local\Temp\.net\CurrentPathTest\l5efxJxdB_e7Z5zW2Hw4J3OtGjuVIsY=\
标签:exe,CurrentPathTest,C#,currentPath,路径,str,测试,AppendAllText,File
From: https://www.cnblogs.com/higleam/p/18011693