如何杀死多个进程?

How to kill multiple processes?

我发现这段代码可以杀死一个进程。但是这只适用于 1 个进程(MS Word)。我想要的是杀死 2 个或更多进程。

  For Each p As Process In Process.GetProcesses
        If String.Compare(p.ProcessName, "WINWORD", True) = 0 Then
            p.Kill()
        End If
    Next

我尝试添加 If String.Compare(p.ProcessName, "WINWORD","EXCEL", True) = 0 Then 但是这个代码是错误的

我也试试这个 If String.Compare(p.ProcessName, "WINWORD , EXCEL", True) = 0 Then 这段代码没问题,但它不能读取 Winword 或 excel

添加更多比较,例如:

if String.Compare(p.ProcessName, "WINWORD", True) = 0 Or String.Compare(p.ProcessName, "EXCEL", True) = 0 Then
    p.Kill()
End If

或者,如果列表更大,您可以使用不同的方法。

Dim procsToKill() As String = {"WINWORD", "EXCEL", "NOTEPAD"} ' Add more tho this list
For Each p As Process In Process.GetProcesses
    If procsToKill.Contains(p.ProcessName) Then
        p.Kill()
    End If
Next