23

I just want to check if any desired application is already running or not

for eg suppose I have VLC or iTunes or any windows application then how can i figure it out by c# code if it is running or not.

Pankaj
  • 1,446
  • 4
  • 19
  • 31

2 Answers2

41

This should be pretty easy to find with a quick Google search, but here you go:

if (Process.GetProcessesByName("process_name").Length > 0)
{
    // Is running
}

Replace process_name with the name of the process you are looking for (i.e. vlc).

Checking for Self-Process

As pointed out in the comments by @filimonic, if you want to check whether more than one instance of the application is running you can use > 1 in place of > 0:

if (Process.GetProcessesByName("process_name").Length > 1)
{
    // Is running
}

This works by checking that a maximum of 1 processes is currently running.

Martin
  • 16,093
  • 1
  • 29
  • 48
  • 5
    You should specify >1 if it is self process – filimonic May 21 '15 at 19:00
  • Just to remove question 'What am I doing wrong, it says another instance is running'. When you start process with name 'proc.exe', you will get Length >= 1, not 0 (process counts itself). Just to make it cleaner – filimonic May 22 '15 at 19:18
  • I guess the `>0` or `>1` is based on if you're checking if another instance of your own application is running already, right? In case of checking if another process is running, I would even suggest using `LinQ`'s `Process.GetProcessesByName("process_name").Any()` which seems cleaner for me. – Gonzo345 Nov 21 '18 at 10:18
  • 4
    This is the google search result :) – Juan May 29 '20 at 18:06
  • Using System.Diagnostics; – Jeff Jan 19 '21 at 21:02
4

you can use either Process.GetProcessesByName if you know the process name or Process.GetProcessesByID if you know it's ID.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70