Get Process Id by Name or Path with .NET

Get Process Id by Name or Path with .NET

Getting the process ID by the name or path of a process can be useful for a variety of reasons, such as being able to kill a hanging process or checking if a specific process is running on a machine. In this blog post, I'll explore how to do this using .NET.

To start, we'll need to add a reference to the System.Diagnostics namespace in our code. This namespace contains various types that allow us to interact with processes running on the machine.

Get the process by name

Once we have added the reference, we can use the Process class to access information about the processes running on the machine. The Process class has a static method called GetProcessesByName which takes a string as an argument and returns an array of Process objects with the specified name. This method is case-insensitive, so it will return processes with names that match the given string regardless of whether the letters are upper or lower case.

Here's an example of how to use this method to get the process ID of a process with a given name:

using System.Diagnostics;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string dummyProcessName = "notepad";

            // Get the process with the specified name
            Process? process = Process.GetProcessesByName(dummyProcessName).FirstOrDefault();

            // Print the PID of the process
            if(pprocess is not null)
            {
                Console.WriteLine("PID: " + process.Id);
            }
            else
            {
                Console.WriteLine("No process with the name " + dummyProcessName + " was found.");
            }
        }
    }
}

Get the process by path

To get the PID by path, you will first need to obtain a list of all running processes on the system. This can be done with the Process.GetProcesses() method, which returns an array of Process objects representing the running processes.

Next, you will need to iterate through the array of Process objects and find the one that has the path you are looking for. This can be done using the Process.MainModule property, which returns a ProcessModule object representing the main module (executable file) associated with the process.

Once you have the ProcessModule object, you can use its FileName property to obtain the path of the associated executable file. You can then compare this path with the one you are looking for, and if they match, you can use the Process.Id property to obtain the PID of the process.

using System.Diagnostics;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the path of the process you want to find
            string path = "C:\MyProcess.exe";

            // Get a list of all the processes currently running on the system
            Process[] processes = Process.GetProcesses();

            // Iterate through the list of processes
            foreach (Process process in processes)
            {
                // Check if the process path matches the one we're looking for
                if (process.MainModule.FileName == path)
                {
                    // If it does, print the process ID to the console
                    Console.WriteLine("Process ID: {0}", process.Id);
                }
            }
        }
    }
}

That's all there is to it! With just a few lines of code, you can easily find the process ID by path using .NET.