首页 > 系统相关 >C#中获取进程当前路径各种方法的测试

C#中获取进程当前路径各种方法的测试

时间:2024-02-08 11:45:10浏览次数:29  
标签:exe CurrentPathTest C# currentPath 路径 str 测试 AppendAllText File

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

相关文章

  • PoloarCTF WriteUp
    PoloarCTFWriteUpWebloginimportreimportrequestsres=""url="http://5077cb7c-84a4-4a16-84e9-8a4547f7efbc.www.polarctf.com:8090"foriinrange(2,30):username="202001{:02d}".format(i)password="202001{......
  • [NCTF2019]Fake XML cookbook
    [NCTF2019]FakeXMLcookbook打卡环境是一个登录页面,根据题目名字就能看出来和xml有关,和xml有关的那就是注入,brup抓包看下数据包,结果如下:发现username和password是xml格式,尝试XML外部实体注入得到flag<?xmlversion="1.0"encoding="utf-8"?><!DOCTYPEnote[<!ENTITYte......
  • 用python写一个并发测试工具
    工作中会有一些需要并发测试的场景,例如:两人同时操作一条数据,此时需要验证结果是否符合预期 最初是借助jmeter来进行并发测试,建2个线程组,每个线程组中各放一个接口,启动时会同时执行个线程组中的接口,从而实现并发测试的目的但是每次都要打开jmeter,用起来不太方便,所以就尝试用pyt......
  • Codeforces-Round-917
    比赛链接A.LeastProduct假如序列中有\(0\),那么答案是\(0\)。否则如果有奇数个负数,只需要让每个数的绝对值最大:不操作是最优的。否则如果有偶数个负数,乘积只会\(\ge0\),为了达到最小值\(0\),只需要将任意一个数改成\(0\)。时间复杂度\(\mathcal{O}(n)\)。codeforA......
  • Codeforces-Pinely-Round-3
    比赛链接A.DistinctButtons扣掉一个方向就是只能走到相邻两个象限以及和这两个象限相邻的坐标轴上,判一下是不是所有的点都满足就行。判的时候只需要判是否所有\(x\le0\)(落到二、三象限),或者所有\(x\ge0\)(四、一象限),或者所有\(y\le0\)或者所有\(y\ge0\)就行,......
  • 前端vscode+eslint代码规范
    前端使用vscode+eslint格式化规范代码格式在应用商店安装eslint,配置好eslint,根目录新增.eslintrc.jsmodule.exports={root:true,parserOptions:{parser:'babel-eslint',sourceType:'module'},env:{browser:true,node:true,es6......
  • GCC版本升级
    GCC版本升级因ubuntu版本比较旧,而项目需要更高版本的gcc,故需升级。这里记录源码升级gcc的过程。由于直接下载gcc源码后编译会出现依赖报错,这里直接先记录编译依赖的过程,具体有以下三个依赖:gmpmpfrmpc笔者的编译目录为/home/ubuntu/gcc,故编译指令中的路径,请读者自行替换。......
  • [ABC327G] Many Good Tuple Problems 题解
    Description对于一对长度均为\(M\)且元素值在\(\left[1,N\right]\)之间的序列\((S,T)\),定义其为好的当且仅当:存在一个长度为\(N\)的\(01\)序列\(X\),使得其满足如下条件:对于任意\(i\in\left[1,M\right]\),有\(X_{S_i}\neqX_{T_i}\)。给定\(N,M\),求在......
  • C#对window 硬件类操作 ManagementObjectSearcher
    在解决方案--引用--右键--添加引用---程序集---框架---勾选System.Management 前言: 我们在很多情况下想要获得计算机的硬件或操作系统的信息,比如获得CPU序列号给自己的软件添加机器码锁绑定指定电脑。又或者想要获得硬盘分区,声卡显卡等信息。开篇:我们用到的主要类是Managem......
  • FSCapture如何将照片改为固定的尺寸和像素大小
    制作要求:原始照片像素为2134*3048;比如要求尺寸2.5*3.5cm(295*413像素),格式仅限JPG、png和JPEG,大小300KB以内(不包含)。FSCapture操作过程:(1)修正比例。打开FSCapture后,将照片拖入该软件内,计算高宽比为3.5/2.5=1.4(不要循环小数,目的是比例应一致),像素数必须是整数,基于这个原则,2134相......