Question : How to get "User Name" in TaskManager process?

I want to find get all the notepad.exe processes that are running (showing in TaskManager), and kill selected ones based on the "User Name".  Below I am just running a test to get the Process Name and ID, and I can get them, but I don't see a property for the "User Name".  How do I get the "User Name"?  I am using .NET 2.0 and vb.net in Visual Studio 2005.  Thank you for your help.

Imports System.Management
Imports System.Data.SqlClient
Imports System.Text
Public Function ListProcesses() As StringBuilder
    Dim strBr As StringBuilder = New StringBuilder
    Dim processList() As System.Diagnostics.Process
    processList = Process.GetProcesses

    For Each P As Process In processList
        If P.ProcessName = "notepad" Then  
                    If P.UserName ????????????????????????????????  Then  'I cannot get User Name.
                  strBr.AppendLine(P.ProcessName.ToString)
                  strBr.AppendLine(P.Id.ToString)
            End If
        End If
    Next
    Return strBr
End Function

Answer : How to get "User Name" in TaskManager process?

Okey dokey... I can do that.

Here is a Quick-n-Dirty example...

' Add a reference to System.Management
Imports System.Management

Public Class Form1

    ' Imagine a form1 with two textboxes and a button
    '   tbProcessName
    '   tbProcessOwner
    '   bnKill

    Private Sub bnKill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnKill.Click
        Dim wmi As New ManagementClass("Win32_Process")
        Dim obj As ManagementObject
        Dim ProcessID, ret As Integer
        Dim ProcessName, ProcessOwner, names(0) As String

        'loop thru the list of process
        For Each obj In wmi.GetInstances
            ' get the Process ID (we'll don't need it, but it's handy)
            ProcessID = Convert.ToInt32(obj("ProcessID"))
            ' get the Process Name
            ProcessName = obj("Description").ToString
            ' get the Process Owner (this might fail if the process has already exited)
            If Convert.ToInt32(obj.InvokeMethod("GetOwner", names)) = 0 Then
                ProcessOwner = names(0)
            Else
                ProcessOwner = "none"
            End If

            Debug.WriteLine("PID=" & ProcessID & ", Name='" & ProcessName & "', Owner='" & ProcessOwner & "'")

            ' OK, let's see if they match the one we're looking for (it's case senstitive)
            If ProcessName = tbProcessName.Text And ProcessOwner = tbProcessOwner.Text Then
                Debug.WriteLine("Yep, that's the one we're looking for...")

                ret = Convert.ToInt32(obj.InvokeMethod("Terminate", Nothing))
                ' these return codes are right out of the documentation
                Select Case ret
                    Case 0
                        MsgBox("Thread terminated successfully")
                    Case 2
                        MsgBox("Access denied")
                    Case 3
                        MsgBox("Insufficient privilege")
                    Case 8
                        MsgBox("Unkown failure")
                    Case 9
                        MsgBox("Path not found")
                    Case 21
                        MsgBox("Invalid Parameter")
                    Case Else
                        MsgBox("Error code " & ret)
                End Select
                Exit For
            End If
        Next

        MsgBox("Done")
    End Sub
End Class
Random Solutions  
 
programming4us programming4us