How Can I Determine if a Specific Process is Running in C#?
Jan 15, 2025 pm 09:22 PMCheck the process running status
The Process class in the System.Diagnostics namespace represents a running process. It provides a variety of process operation methods, including querying process status. When accessing a Process reference, it is critical to determine whether it is running.
Use ProcessName method
One way to check the status of a process is to use the GetProcessesByName method:
Process[] pname = Process.GetProcessesByName("notepad"); if (pname.Length == 0) MessageBox.Show("未运行"); else MessageBox.Show("正在运行");
This code retrieves all processes with a specified name (e.g., "notepad"). If the length of the result array is zero, it means that no process of that name is running; otherwise, it means that there is currently a process running.
Traverse the process
Another way is to loop through all running processes and check their IDs:
Process[] processlist = Process.GetProcesses(); foreach(Process theprocess in processlist){ Console.WriteLine("进程:{0},ID:{1}", theprocess.ProcessName, theprocess.Id); }
This method iterates over all active processes, displaying their names and IDs. It provides a more comprehensive view of running system processes.
The above is the detailed content of How Can I Determine if a Specific Process is Running in C#?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the types of values returned by c language functions? What determines the return value?

What are the definitions and calling rules of c language functions and what are the

C language function format letter case conversion steps

Where is the return value of the c language function stored in memory?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

How does the C Standard Template Library (STL) work?
