如何杀死多个进程?

时间:2016-12-12 06:26:25

标签: vb.net process task

我发现这个代码会杀死一个进程..但是这只适用于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

1 个答案:

答案 0 :(得分:2)

添加更多比较,例如:

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