Kill Several Processes In VB?
-
30-05-2021 - |
Question
I currently have the following up in VS 2010
Dim myProcess() As Process = System.Diagnostics.Process.GetProcessesByName("calc")
For Each myKill As Process In myProcess
myKill.Kill()
However I cannot seem to get it to kill more than one process. Example I've tried
("calc",mspaint")
("calc,mspaint")
("calc"),("mspaint")
Any ideas? Thanks for your time/support
Solution
Quote from MSDN documentation of GetProcessByName:
Creates an array of new Process components and associates them with the existing process resources that all share the specified process name.
You can't pass an array of arguments to it.Instead,you can get all the processes using the GetProcesses method,iterate over all processes and check if its name match one of the names you want:
Dim procs = System.Diagnostics.Process.GetProcesses().Where((Function(p) p.ProcessName = "calc" Or p.ProcessName = "mspaint"))
For Each p As Process In procs
p.Kill()
Next
Non-LINQ way:
For Each p As Process In Process.GetProcesses()
If p.ProcessName = "calc" Or p.ProcessName = "mspaint" Then
p.Kill()
End If
Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow