Home >Backend Development >C++ >Is a Specific Process Running in C#?
How to determine the running status of a process
When dealing with processes in C#, it is crucial to determine whether a specific process is running. The System.Diagnostics.Process
class provides methods to determine this status.
One way is to retrieve a list of processes matching a specified name:
<code class="language-csharp">Process[] pname = Process.GetProcessesByName("notepad"); if (pname.Length == 0) MessageBox.Show("进程未运行"); else MessageBox.Show("进程正在运行");</code>
Alternatively, you can loop through all running processes to get their process IDs:
<code class="language-csharp">Process[] processlist = Process.GetProcesses(); foreach(Process theprocess in processlist){ Console.WriteLine("进程名称: {0},ID: {1}", theprocess.ProcessName, theprocess.Id); }</code>
By leveraging these methods, you can effectively determine whether a specified process is running.
The above is the detailed content of Is a Specific Process Running in C#?. For more information, please follow other related articles on the PHP Chinese website!