英文:
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.GetProcessById
、Process.GetProcessesByName
等)来获取它。
英文:
You're looking to do some sort of lookup, a Dictionary is a good tool for that.
Dictionary<string, Process> processNames = new();
Add and remove entries just like any other Dictionary.
Process p = ...
processNames.Add("myprocess", p);
then look it up by it's name
Process p2 = processNames["myprocess"];
You can populate p
with Process.Start
, or any other "GetProcess*" static methods in the Process class (Process.GetProcessById
, Process.GetProcessesByName
, etc).
答案2
得分: 0
你会想要重新命名你的项目以更改进程名称。进程名称只是正在运行的可执行文件的名称。每当你构建时,主要的结果文件将与项目名称相同。看一下:
你可以做的一件事是使用哈希集或字典来跟踪你创建的进程,如果你在做任何涉及到特定进程的 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:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论