Khushi
Khushi

Reputation: 49

Kill a running batch file from C#

Is there a way to kill a batch process which is not started from C# code to kill from c# code. i.e. I have start.bat which is running and i want to kill it when a WPF app starts. Is this possible?? The WPF shall kill only start.at and not other batch running.

Upvotes: 2

Views: 2532

Answers (2)

Knuckle-Dragger
Knuckle-Dragger

Reputation: 7066

From powershell I find the PID of Start.bat like this.

(gwmi win32_process | where {$_.commandline -match "START.BAT"} | select processID).processID

Invoke powershell from C# or just contact WMI directly, easy to find if you look at Win32_Process commandline and processID properties.

Upvotes: 1

Yanshof
Yanshof

Reputation: 9926

 int processID = ... 
 // you also can use    Process.GetProcessesByName(....) to find the proicessID - or the batch process

 //Later when you need to kill the process...
 Process pToKill = Process.GetProcessById(processID);
 pToKill.Kill();

Upvotes: 2

Related Questions