Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Get Process Id by Name or Path with .NET

Get Process Id by Name or Path with .NET
December 14, 2022 3 min read BEN ABT
.NET

This article was written in 2022. Versions, tools and recommendations may have changed since then. Newer posts are collected under .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:

C#
 1using System.Diagnostics;
 2
 3namespace MyApp
 4{
 5    class Program
 6    {
 7        static void Main(string[] args)
 8        {
 9            string dummyProcessName = "notepad";
10
11            // Get the process with the specified name
12            Process? process = Process.GetProcessesByName(dummyProcessName).FirstOrDefault();
13
14            // Print the PID of the process
15            if(pprocess is not null)
16            {
17                Console.WriteLine("PID: " + process.Id);
18            }
19            else
20            {
21                Console.WriteLine("No process with the name " + dummyProcessName + " was found.");
22            }
23        }
24    }
25}

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.

C#
 1using System.Diagnostics;
 2
 3namespace MyApp
 4{
 5    class Program
 6    {
 7        static void Main(string[] args)
 8        {
 9            // Get the path of the process you want to find
10            string path = "C:\MyProcess.exe";
11
12            // Get a list of all the processes currently running on the system
13            Process[] processes = Process.GetProcesses();
14
15            // Iterate through the list of processes
16            foreach (Process process in processes)
17            {
18                // Check if the process path matches the one we're looking for
19                if (process.MainModule.FileName == path)
20                {
21                    // If it does, print the process ID to the console
22                    Console.WriteLine("Process ID: {0}", process.Id);
23                }
24            }
25        }
26    }
27}

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.

Related articles

Production Rate Limiting in ASP.NET Core

Nov 3, 2026 - 16 min read

Production Rate Limiting in ASP.NET Core

Most ASP.NET Core rate-limiting examples stop after registering a fixed window. That is enough to demonstrate the middleware, but it is not …

The Big Decimal Bugfix with .NET 11

Nov 2, 2026 - 11 min read

The Big Decimal Bugfix with .NET 11

For more than two decades, (decimal)1.23 returned 1.23m. It looked correct, it passed every test and it was a polite lie.

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026 - 10 min read

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled and Alpine are often compared as three equivalent Linux distributions. They are not. Alpine is a distribution built …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Automating Microsoft Entra External ID: CIAM Provisioning and Branding

Automating Microsoft Entra External ID: CIAM Provisioning …

Nov 10, 2026
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (42) DevOps (17) Docker (15) Microsoft (14) Security (12) EF Core (11) Microsoft MVP (11) AI (10) GitHub (10) GitHub Copilot (10) Architecture (9) ASP.NET (9) Synology (9) .NET 11 (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact