在vb.net中杀死进程

时间:2015-09-30 14:09:43

标签: vb.net

我实际上要做的是杀死所有常见进程。 EX:chrome,firefox等。

我期待做的是:'除了[需要的'之外'杀死所有进程。 我想在这里用这段代码替换它。

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Button1.Enabled = False
    Label3.ForeColor = Color.Green
    Timer1.Start()

    If ProgressBar1.Value = 100 Then
        MessageBox.Show("Done", "Kill Successfull")
    End If

End Sub


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Button1.Enabled = True
    Label3.ForeColor = Color.Red
    Timer2.Stop()
    ProgressBar1.Value = 0
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    For Each prog As Process In Process.GetProcesses
        Timer2.Start()
        ProgressBar1.Increment(30)
        If prog.ProcessName = "chrome" Then
            prog.Kill()
        End If
        If prog.ProcessName = "notepad" Then
            prog.Kill()
        End If
        If prog.ProcessName = "firefox" Then
            prog.Kill()
        End If
        If prog.ProcessName = "codeblocks" Then
            prog.Kill()
        End If
        If prog.ProcessName = "cmd" Then
            prog.Kill()
        End If
        If prog.ProcessName = "PhotoshopCS6" Then
            prog.Kill()
        End If
        If prog.ProcessName = "uTorrent" Then
            prog.Kill()
        End If
        If prog.ProcessName = "ccleaner" Then
            prog.Kill()
        End If
        If prog.ProcessName = "notepad++" Then
            prog.Kill()
        End If
        If prog.ProcessName = "lightshot" Then
            prog.Kill()
        End If
        If prog.ProcessName = "ccleaner" Then
            prog.Kill()
        End If
    Next
End Sub

1 个答案:

答案 0 :(得分:1)

我认为最好将其视为杀死我不需要的所有过程。原因是如果你偶然杀死它们需要很多进程,可能会导致崩溃。

尝试维护您通常打开的所有程序的列表以及它们的进程名称,然后我将更改您的代码以迭代数组。稍后,您可以向程序添加控件,以便稍后对此数组进行更改。如果您需要添加要杀死的程序,这将使您以后更容易阅读代码。

Dim lstOprograms As String() = {
     "chrome",
     "notepad",
     "firefox",
     "codeblocks"}
'' etc. Until you have all your program names
For Each prog As Process In Process.GetProcesses
        Timer2.Start()
        ProgressBar1.Increment(30)
        For Each nm As String In lstOprograms
           If prog.ProcessName = nm Then
               prog.Kill()
               Exit For
           End If
        Next
Next