2

我正在使用iexplore终止终端服务器上的进程(Internet Explorer)的应用程序。我遇到的问题是我杀死了终端服务器上 Internet Explorer 的所有进程,而不仅仅是当前用户的进程。

因此,如果我以身份登录User1并杀死 IE,它将被杀死User2User3依此类推……我只想User1杀死 的 Internet Explorer。我使用以下代码终止我的进程:

Private Sub ClearProcesses(ByVal ProcessName As String)
    Dim myProcesses = Process.GetProcessesByName(ProcessName)
    For Each Proc As Process In myProcesses
        Try
            Proc.Kill()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "ClearProcess")
        End Try
    Next
End Sub

有没有办法只杀死进程User1

4

1 回答 1

3

您可以使用 WMI 获取进程的所有者,并验证该进程是否为当前用户所有。

使用这样的函数:

Public Function GetProcessOwner(processId As Integer) As String
    Dim query As String = "Select * From Win32_Process Where ProcessID = " + processId
    Dim searcher As New ManagementObjectSearcher(query)
    Dim processList As ManagementObjectCollection = searcher.[Get]()

    For Each obj As ManagementObject In processList
        Dim argList As String() = New String() {String.Empty, String.Empty}
        Dim returnVal As Integer = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList))
        If returnVal = 0 Then
            ' argList(0) == User
            ' argList(1) == DOMAIN
            Return argList(0)
        End If
    Next

    Return "NO OWNER"
End Function

您应该能够执行以下操作:

Private Sub ClearProcesses(ByVal ProcessName As String)
    Dim myProcesses = Process.GetProcessesByName(ProcessName).Where(Function(p) GetProcessOwner(p.Id) = currentUser)

    ' Your current code...
于 2013-06-04T16:57:43.020 回答