Home >Backend Development >C++ >Is a Specific Process Running in C#?

Is a Specific Process Running in C#?

Barbara Streisand
Barbara StreisandOriginal
2025-01-15 21:41:48469browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn