Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if application is running [duplicate]

Tags:

c#

process

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.

like image 469
Pankaj Avatar asked Aug 02 '13 10:08

Pankaj


People also ask

How can I tell if an EXE is running C#?

string fileName = Path. GetFileName(path); // Get the precess that already running as per the exe file name. ' Pass your exe file path here.

How do I know if an application is running or not?

The best place to start when monitoring apps is the Task Manager. Launch it from the Start menu or with the Ctrl+Shift+Esc keyboard shortcut. You'll land on the Processes screen. At the top of the table, you'll see a list of all the apps which are running on your desktop.


2 Answers

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.

like image 117
Martin Avatar answered Nov 15 '22 17:11

Martin


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

like image 44
No Idea For Name Avatar answered Nov 15 '22 18:11

No Idea For Name