How to get process handle from process name in C#
By FoxLearn 2/11/2025 4:03:57 AM 656
The System.Diagnostics.Process
class provides a method called GetProcessesByName()
, which retrieves all processes with a given name. The parameter passed to this method should be the process's name without the .exe
extension. Since multiple processes can share the same name, the method returns an array of Process
objects, potentially containing more than one process instance.
How to get process handle or process id from running process name in C#?
The Id
and Handle
properties in the Process
class represent the native process ID (PID) and the native handle value, respectively. The Id
is a unique identifier for the process in the operating system, while the Handle
is a pointer to the native handle associated with the process.
For example, c# get process id by name
// get notepad.exe processes using c# getprocessesbyname var processes = Process.GetProcessesByName("notepad"); foreach (var process in processes) { // c# get process id Console.WriteLine("PID = {0}", process.Id); // c# getprocesshandle Console.WriteLine("Process Handle = {0}", process.Handle); }
In this example:
- Using
Process.GetProcessesByName(processName) to retrieve
an array ofProcess
objects that match the given name. If no processes with the specified name are found, it returns an empty array. - Next, Use
process.Handle
property of theProcess
class gives you a pointer (IntPtr) to the process handle. However, accessing the handle might require elevated privileges depending on the system configuration and the target process.
C# Get Process name
To get the name of a process in C#, you can use the System.Diagnostics
namespace and its Process
class.
For example, c# process name
// Get the current process Process currentProcess = Process.GetCurrentProcess(); // Get the process name (without the extension) string processName = currentProcess.ProcessName; Console.WriteLine("Current process name: " + processName);
C# Get Process Id
For example, Get the name of a specific process by its process ID
int processId = 1234; // Replace with the actual process ID // Get the process by ID Process process = Process.GetProcessById(processId); // c# get process name by id // Get the process name (without the extension) // c# process name string processName = process.ProcessName; Console.WriteLine("Process name for ID " + processId + ": " + processName);
In these examples:
Process.GetCurrentProcess().ProcessName
gives you the name of the current process.Process.GetProcessById(processId).ProcessName
retrieves the name of a process by its ID.
If you want to get a list of all running processes and their names, you can use Process.GetProcesses()
.
- How to use JsonConverterFactory in C#
- How to serialize non-public properties using System.Text.Json
- The JSON value could not be converted to System.DateTime
- Try/finally with no catch block in C#
- Parsing a DateTime from a string in C#
- Async/Await with a Func delegate in C#
- How to batch read with Threading.ChannelReader in C#
- How to ignore JSON deserialization errors in C#