Getting The Process ID for an Executable Path

H

Hayato Iriumi

Hello,

I have a need to kill a process. It sounds each enough, but the problem is
that I have multiple executables running from different paths. Say, I have
MyService.exe running from C:\FolderA and the same MyService.exe running in
C:\FolderB. I want to kill the process that is running under C:\FolderA. So
I guess I have a need to get the Process ID for the executable under
C:\FolderA.

How can I kill the process when you know the path to the executable?

TIA
 
D

Derek Griffiths

I'd use the registry or a textfile. Have each process create a key with
its' path and use the process ID for the value (after it's started). When
you kill the process delete the key. If you want to use a textfile, then
put it in %tmp% and have it track the path of all executables and process
ids. The textfile is probably the more difficult route.
 
H

Hayato Iriumi

ew... I personally don't like that.

I did though came up with this piece of code. Answer to my own question...

public static void KillProcess(string ExecutablePath)

{

string strTargetProcessName =
System.IO.Path.GetFileNameWithoutExtension(ExecutablePath);

Process[] Processes = Process.GetProcessesByName(strTargetProcessName);

foreach(Process p in Processes)

{

foreach(ProcessModule m in p.Modules)

{

if(ExecutablePath.ToLower() == m.FileName.ToLower())

{

p.Kill();

return;

}

}


}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top