我正在使用终止服务器上的进程iexplore
(Internet Explorer)的应用程序。我遇到的问题是我终止了终端服务器上的所有Internet Explorer进程,而不仅仅是当前用户的进程。
因此,如果我以User1身份登录并杀死IE,它将被杀死User2,User3等等......我只希望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的进程?
答案 0 :(得分: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...