リモートデスクトップサービス環境で動作させるため
特定ユーザーのプロセスを殺す事が必要になりました。
調べながら作ってみたのでメモ程度に。
Dim strUserName As String = Environment.UserName
'' WMIからプロセス検索
Dim query As ManagementObjectSearcher = New ManagementObjectSearcher("SELECT * FROM Win32_Process Where Name = 'hoge.exe'")
Dim col As ManagementObjectCollection = query.Get()
For Each o As ManagementObject In col
'' プロセスIDの取得
Dim pId As Integer = Integer.Parse(o("ProcessId").ToString)
Dim userInfo As Object() = New Object(1) {}
'' 所有者の取得
o.InvokeMethod("GetOwner", userInfo)
'' ユーザーが一致する場合のみプロセスをKillする
If strUserName.Equals(CType(userInfo(0), String)) Then
Dim proc As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessById(pId)
proc.Kill()
Exit For
End If
Next
※参照とインポートに「System.Management」が必要
まだ、動作確認してません。
取り敢えず作っただけなのでキレイに書けてません。
特定ユーザーのプロセスを殺す事が必要になりました。
調べながら作ってみたのでメモ程度に。
Dim strUserName As String = Environment.UserName
'' WMIからプロセス検索
Dim query As ManagementObjectSearcher = New ManagementObjectSearcher("SELECT * FROM Win32_Process Where Name = 'hoge.exe'")
Dim col As ManagementObjectCollection = query.Get()
For Each o As ManagementObject In col
'' プロセスIDの取得
Dim pId As Integer = Integer.Parse(o("ProcessId").ToString)
Dim userInfo As Object() = New Object(1) {}
'' 所有者の取得
o.InvokeMethod("GetOwner", userInfo)
'' ユーザーが一致する場合のみプロセスをKillする
If strUserName.Equals(CType(userInfo(0), String)) Then
Dim proc As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessById(pId)
proc.Kill()
Exit For
End If
Next
※参照とインポートに「System.Management」が必要
まだ、動作確認してません。
取り敢えず作っただけなのでキレイに書けてません。