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

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026 - 11 min read

Containerizing .NET in 2026 Without a Dockerfile

Most .NET Dockerfiles repeat the same five steps: restore, publish, select the matching Microsoft runtime image, copy the output and start …

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026 - 12 min read

Problem Details in ASP.NET Core and .NET 11

builder.Services.AddProblemDetails() registers infrastructure. It does not create an error contract. An ASP.NET Core request can fail during …

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026 - 11 min read

Resilience in .NET with Microsoft.Extensions.Resilience

Retries are the most visible resilience strategy and the easiest one to misuse. Repeating a failing call can recover from a short network …


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
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026
Problem Details in ASP.NET Core and .NET 11

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026
Resilience in .NET with Microsoft.Extensions.Resilience

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026
What Matters in ASP.NET Core 11

What Matters in ASP.NET Core 11

Sep 14, 2026

Tags

.NET (192) Csharp (96) ASP.NET Core (48) Azure (41) DevOps (17) Docker (14) Microsoft (14) EF Core (11) Microsoft MVP (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Security (9) Synology (9) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7) Bicep (6)
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