gpt4 book ai didi

vb.net - 如何按用户杀死进程?

转载 作者:行者123 更新时间:2023-12-04 04:54:42 24 4
gpt4 key购买 nike

我正在使用终止进程的应用程序 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 的进程?

最佳答案

您可以使用 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...

关于vb.net - 如何按用户杀死进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16923332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com