杀死远程进程

时间:2014-07-24 18:16:07

标签: vb.net process wmi

我使用了以下链接,并且能够成功设置它: http://www.codeproject.com/Articles/26817/Killing-Processes-from-a-Visual-Basic-Application?msg=4868229#xx4868229xx

我试图弄清楚如何在VB.NET中查看/终止远程工作站的进程与本地进程?有什么想法吗?

我一直在测试WMI,但似乎无法弄明白并让它正常运行。

这是我的班级代码:

Public Class Form1
    Public Sub endprocess(ByVal RemotePC As String, ByVal process As String)
        Dim objWMIService As Object
        Dim colProcessList As Array
        Dim objprocess As Object
        Dim response As Boolean
        Dim pcname As String = tbRemoteIP.Text

        objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & pcname & "\root\cimv2")
        process = "'" & process & "'"
        colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = " & "SDM Offline Tool")
        For Each objprocess In colProcessList
            response = MsgBox("End " & process & " on " & pcname & "?", MsgBoxStyle.YesNoCancel)
            If response = vbYes Then
                objprocess.Terminate()
            Else
                Exit Sub
            End If
        Next
    End Sub

我用以下按钮调用上面的内容:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    endprocess("pcname", "SDM Offline Tool")
End Sub

我在remotepc(pcname)上尝试并结束SDM Offline Tool进程时收到CAST错误

1 个答案:

答案 0 :(得分:1)

尝试使用此代码,它适用于" notepad.exe":

Public Sub endprocess(ipAddress As String, processName As String)
  Dim scope As New ManagementScope("\\" & ipAddress & "\root\cimv2")
  Dim query As New SelectQuery(
    "SELECT * FROM Win32_Process WHERE Name = '" & processName & "'")

  Using searcher As New ManagementObjectSearcher(scope, query)
    Dim queryCollection As ManagementObjectCollection = searcher.[Get]()
    For Each process As ManagementObject In queryCollection
      process.InvokeMethod("Terminate", Nothing)
    Next
  End Using
End Sub

Sub Main()
  endprocess("ipAddress", "notepad.exe")
End Sub

不要忘记添加对System.Management(.NET 4.0)的引用,并在顶部添加Imports