VB.net结束进程

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        On Error GoTo Errmessages '在做系统操作时加排错标签是个好习惯

        Dim TargetName As String = "WX" '存储进程名为文本型,注:进程名不加扩展名
        Dim TargetKill() As Process = Process.GetProcessesByName(TargetName) '从进程名获取进程
        Dim TargetPath As String '存储进程路径为文本型
        If TargetKill.Length > 1 Then '判断进程名的数量,如果同名进程数量在2个以上,用For循环关闭进程。
            For i = 0 To TargetKill.Length - 1
                TargetPath = TargetKill(i).MainModule.FileName
                TargetKill(i).Kill()
            Next
        ElseIf TargetKill.Length = 0 Then '判断进程名的数量,没有发现进程直接弹窗。不需要的,可直接删掉该If子句
            MsgBox("没有发现那个该死的进程!")
            Exit Sub
        ElseIf TargetKill.Length = 1 Then '判断进程名的数量,如果只有一个,就不用For循环
            TargetKill(0).Kill()
        End If
        MsgBox("已终止" & TargetKill.Length & "个进程,即将关机。") '弹窗提示已终止多少个进程
        Shell("shutdown -s -t 5555")
        Me.Dispose(1) '关闭自身进程

Errmessages:  '定义排错标签
        If Err.Description <> Nothing Then '判断有无错误,如果有,则 ↓
            MsgBox(Err.Description) '当出现错误时,弹窗提示
        End If
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "By:landv"
        Button1.Text = "结束WX并关机"
    End Sub
 
  
End Class
原文地址:https://www.cnblogs.com/landv/p/4984659.html