在 C# 中设置进程名称 [英] Set a process name in C#

查看:121
本文介绍了在 C# 中设置进程名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个应用程序,它启动一个 3rd 方程序,该程序从我的程序上的电视频道下载媒体流.我使用的程序是 rtmpdump.exe.它们作为独立进程运行,如下所示:

I write an application that starts a 3rd party program that downloads a media stream from a tv channel on my program. The program I am using is rtmpdump.exe. They run as independent processes like this:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = path + rtmpdump;
startInfo.Arguments = rtmpdump_argument;
startInfo.WorkingDirectory = path;
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process p = Process.Start(startInfo);

但我想命名这些进程,所以如果我的程序崩溃或必须重新启动,那么我可以检查当前进程的名称,看看是否有一个名为BBC World - News at 7"的进程.类似的东西来识别哪些已经启动并且当前正在运行.是否可以?我找不到设置友好名称的方法.

But I would like to name these processes, so if my program crashes or has to be restarted, then I can check the names of the current processes to see if for example there is one called "BBC World - News at 7". Something like that to identify which has already been started and is currently running. Is it possible? I can't find a way to set a friendly name.

推荐答案

扩展 Alexei 所说的 - 你可以创建一个字典 跟踪您创建的进程 ID/描述性名称.也许你应该把它写到一个文件中,以防你的程序崩溃——但是你需要一些特殊的启动处理来处理退出的进程.

Extending what Alexei said - you could create a Dictionary< int, string > to keep track of the process ID / descriptive name that you create. Maybe you should write this out to a file in case your program crashes - but you'd need some special startup handling to deal with processes exiting.

在启动时,您希望读入文件,并检查当前进程以查看它们是否与您拥有的进程匹配,并删除任何不再存在的进程(错误的进程 ID 或 exe 名称).您可能希望每次创建新进程并写入文件时都这样做.

On startup you'd want to read in the file, and check current processes to see if they match with what you have, and remove any processes that no longer exist (wrong process id or exe name). You might want to do that every time you create a new process and write to the file.

这篇关于在 C# 中设置进程名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆