Search code examples
c#batch-filekill-process

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.


Solution

  • 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.