如何在C#中更改进程名称

huangapple go评论59阅读模式
英文:

How to change process name in C#

问题

我有多个进程同时运行我的C#程序,而且所有这些进程都具有相同的名称。

我正在尝试使用一个函数为此程序运行的每个进程赋予特定的名称。

英文:

I have multiple processes that running at the same time of my C# program and all of this processes have the same name.

I am trying to give specific name to each process running of this program using a function.

答案1

得分: 1

如果你想要进行某种查找操作,使用Dictionary是一个不错的工具。

Dictionary<string, Process> processNames = new();

就像处理其他Dictionary一样,可以添加和删除条目。

Process p = ...
processNames.Add("myprocess", p);

然后可以通过它的名称查找它。

Process p2 = processNames["myprocess"];

你可以使用Process.Start来填充p,或者使用Process类中的任何其他"GetProcess*"静态方法(Process.GetProcessByIdProcess.GetProcessesByName等)来获取它。

英文:

You're looking to do some sort of lookup, a Dictionary is a good tool for that.

Dictionary&lt;string, Process&gt; processNames = new();

Add and remove entries just like any other Dictionary.

Process p = ...
processNames.Add(&quot;myprocess&quot;, p);

then look it up by it's name

Process p2 = processNames[&quot;myprocess&quot;];

You can populate p with Process.Start, or any other "GetProcess*" static methods in the Process class (Process.GetProcessById, Process.GetProcessesByName, etc).

答案2

得分: 0

你会想要重新命名你的项目以更改进程名称。进程名称只是正在运行的可执行文件的名称。每当你构建时,主要的结果文件将与项目名称相同。看一下:

如何在C#中更改进程名称

你可以做的一件事是使用哈希集或字典来跟踪你创建的进程,如果你在做任何涉及到特定进程的 Process 对象的事情。

英文:

You will want to rename your project in order to change the process name. Process names are just the names of the executable file being run. Whenever you build, the main resulting file will be the same as the project name. Take a look:

如何在C#中更改进程名称

One thing you could do is to track processes that you created using a hash set or dictionary if you are doing anything that involves requiring the Process object of that specific process.

huangapple
  • 本文由 发表于 2023年2月6日 20:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定