Process in C# (Open exe)
-
Hi Guy, I need to open exe through C#. I am able to open exe by writing code System.Diagnostics.Process.Start("C:\Program Files\abc\xyz.exe"); but now I need to check if it is already open then no need to run this code because it will reopen. basically need to check whether its open or not, open only if not open.... Thanks&Regards Shafiq...
-
Hi Guy, I need to open exe through C#. I am able to open exe by writing code System.Diagnostics.Process.Start("C:\Program Files\abc\xyz.exe"); but now I need to check if it is already open then no need to run this code because it will reopen. basically need to check whether its open or not, open only if not open.... Thanks&Regards Shafiq...
-
Hi Guy, I need to open exe through C#. I am able to open exe by writing code System.Diagnostics.Process.Start("C:\Program Files\abc\xyz.exe"); but now I need to check if it is already open then no need to run this code because it will reopen. basically need to check whether its open or not, open only if not open.... Thanks&Regards Shafiq...
Try following code...
//Namespaces we need to use
using System.Diagnostics;public bool IsProcessOpen(string name)
{
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses()) {
//now we're going to see if any of the running processes
//match the currently running processes. Be sure to not
//add the .exe to the name you provide, i.e: NOTEPAD,
//not NOTEPAD.EXE or false is always returned even if
//notepad is running.
//Remember, if you have the process running more than once,
//say IE open 4 times the loop thr way it is now will close all 4,
//if you want it to just close the first one it finds
//then add a return; after the Kill
if (clsProcess.ProcessName.Contains(name))
{
//if the process is found to be running then we
//return a true
return true;
}
}
//otherwise we return a false
return false;
}Jinal Desai - LIVE Experience is mother of sage....
-
Try following code...
//Namespaces we need to use
using System.Diagnostics;public bool IsProcessOpen(string name)
{
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses()) {
//now we're going to see if any of the running processes
//match the currently running processes. Be sure to not
//add the .exe to the name you provide, i.e: NOTEPAD,
//not NOTEPAD.EXE or false is always returned even if
//notepad is running.
//Remember, if you have the process running more than once,
//say IE open 4 times the loop thr way it is now will close all 4,
//if you want it to just close the first one it finds
//then add a return; after the Kill
if (clsProcess.ProcessName.Contains(name))
{
//if the process is found to be running then we
//return a true
return true;
}
}
//otherwise we return a false
return false;
}Jinal Desai - LIVE Experience is mother of sage....
-
Hey Jinal Desai Where i need to give the path of my exe file which i need to open and can check that particular process running or not instead of checking all running process on the computer.
-
You did not want to give path of your exe file, just give the name of it.
Jinal Desai - LIVE Experience is mother of sage....
-
-
Hi Guy, I need to open exe through C#. I am able to open exe by writing code System.Diagnostics.Process.Start("C:\Program Files\abc\xyz.exe"); but now I need to check if it is already open then no need to run this code because it will reopen. basically need to check whether its open or not, open only if not open.... Thanks&Regards Shafiq...
Have a look at this snippet. Just change "notepad" (which I used here) to your process name (xyz?).
List<Process> pList = new List(Process.GetProcesses());
Process pSearch = pList.Find( delegate(Process p)
{ return p.ProcessName.ToLower() == "notepad";
} );If
pSerach
isnull
Notepad is not running, otherwise you retrieve theProcess object
for Notepad. [EDIT] Nah, just look atProcess.GetProcessesByName(string)
. No need to make it more complicated than that I suppose.. :)