locked
List processes for the current user in VB RRS feed

  • Question

  • I would like to list the processes that the current user is running, but for now I am listing the ALL running processes. Can somebody help me?

    Here's the code I have now. I'm adding the process names in the combobox, so that users can select them.

            Dim a As Process
            For Each a In Process.GetProcesses()
                ComboBox1.Items.Add(a.ProcessName)

    Wednesday, December 26, 2012 10:03 PM

Answers

  • I think that this gets the most where you are, I created it by some searching on Internet.

            Dim searcher As New ManagementObjectSearcher("Select * from Win32_Process")
            Dim processList As ManagementObjectCollection = searcher.Get()
            For Each obj As ManagementObject In processList
                Dim owner = {""}
                Dim returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", owner))
                If returnVal = 0 Then
                    If owner(0) = Environment.UserName Then
                        ListBox1.Items.Add(obj.Item("Name"))
                    End If
                End If
            Next


    Success
    Cor

    • Marked as answer by Rionto Thursday, December 27, 2012 3:18 PM
    Thursday, December 27, 2012 1:07 PM
  • This pretty much does what you want.

    Imports System.Diagnostics
    Imports System.Management    'Add reference Assemblies, Framework, System.Management
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListView1.AllowColumnReorder = True
            ListView1.GridLines = True
            ListView1.View = View.Details
            ListView1.Sorting = SortOrder.Ascending
    
            ListView1.Columns.Add("Process ID", 100, HorizontalAlignment.Left)
            ListView1.Columns.Add("Process Name", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("User Name", 100, HorizontalAlignment.Left)
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim psList() As Process
            Try
                psList = Process.GetProcesses()
    
                For Each p As Process In psList
                    If GetProcessUserName(p) = Environment.UserName Then
                        Dim item1 As New ListViewItem(p.Id.ToString(), 0)
                        item1.SubItems.Add(p.ProcessName)
                        item1.SubItems.Add(Environment.UserName)
                        ListView1.Items.AddRange(New ListViewItem() {item1})
                    End If
                Next p
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Function GetProcessUserName(ByVal Process As Process) As String
            Dim sq As New ObjectQuery("Select * from Win32_Process Where ProcessID = '" & Process.Id & "'")
            Dim searcher As New ManagementObjectSearcher(sq)
    
            If searcher.Get.Count = 0 Then Return Nothing
    
            For Each oReturn As ManagementObject In searcher.Get
                Dim o As String() = New String(1) {}
    
                'Invoke the method and populate the o var with the user name and domain                                           
                oReturn.InvokeMethod("GetOwner", DirectCast(o, Object()))
    
                Return o(0)
            Next
        End Function
    
        Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
    
        End Sub
    
    End Class
    
    


    You've taught me everything I know but not everything you know.

    • Proposed as answer by Cor Ligthert Thursday, December 27, 2012 2:19 PM
    • Marked as answer by Rionto Thursday, December 27, 2012 3:18 PM
    Thursday, December 27, 2012 2:06 PM
  • Like this:

    If owner(0) = Environment.UserName Then
    	Dim shortName = IO.Path.GetFileNameWithoutExtension(obj.Item("Name"))
    	ListBox1.Items.Add(shortName)
    End If
    

    HTH,

    Andrew

    • Marked as answer by Rionto Wednesday, January 2, 2013 9:11 PM
    Wednesday, January 2, 2013 8:24 PM

All replies

  • With the limited information, it is hard to tell what the problem is. If there is an error, please specify it. Try this code, even though it is similar:

    For Each a As Process In Process.GetProcesses()
    
    ComboBox1.Items.Add(a.ProcessName.ToString) 'This ToString might be the problem
    
    Next

    As already stated, maybe it is that the ProcessName must be converted to a string.
    • Edited by Bill Wohlers Wednesday, December 26, 2012 10:18 PM
    Wednesday, December 26, 2012 10:17 PM
  • If you can find the sessionid for the current user, then you can filter the processes on that.

    --
    Andrew

    Wednesday, December 26, 2012 10:37 PM
  • I guess you could try filtering based on session ID, here's an example:

    I tested it and it gave 100 results here (Process.GetProcesses() gave 155 results):

            Dim AllP() As Process = Process.GetProcesses()
            Dim CurP As Process = Process.GetCurrentProcess()
            Dim lst = (From x As Process In AllP Where x.SessionId = CurP.SessionId Select x).ToArray()
            For Each p As Process In lst
                ComboBox1.Items.Add(p.ProcessName)
            Next

    PS. If you haven't already, you should set the Sorted property of the ComboBox to True, that will make it sort the list alphabetically and make it look much nicer!


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
    Check out my development so far!

    • Proposed as answer by Venkat786 Thursday, December 27, 2012 1:47 AM
    • Marked as answer by Rionto Thursday, December 27, 2012 11:23 AM
    • Unmarked as answer by Rionto Thursday, December 27, 2012 11:28 AM
    Thursday, December 27, 2012 1:17 AM
  • This seems to work, found it on the net and changed it to use ListBoxes. I'm sure you can cut it down to only display current users running processes.

    Imports System.Diagnostics
    Imports System.Management    'Add reference Assemblies, Framework, System.Management
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Label1.Text = "All processes on computer"
            Label2.Text = "Processes owned by user " & Environment.UserName
            ListBox1.Sorted = True
            ListBox2.Sorted = True
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim psList() As Process
            Try
                psList = Process.GetProcesses()
    
                For Each p As Process In psList
                    ListBox1.Items.Add(p.Id.ToString() & "    " & p.ProcessName)
                    If GetProcessUserName(p) = Environment.UserName Then ListBox2.Items.Add(p.Id.ToString() & "    " & p.ProcessName & "    " & Environment.UserName)
                Next p
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Function GetProcessUserName(ByVal Process As Process) As String
            Dim sq As New ObjectQuery("Select * from Win32_Process Where ProcessID = '" & Process.Id & "'")
            Dim searcher As New ManagementObjectSearcher(sq)
    
            If searcher.Get.Count = 0 Then Return Nothing
    
            For Each oReturn As ManagementObject In searcher.Get
                Dim o As String() = New String(1) {}
    
                'Invoke the method and populate the o var with the user name and domain                                           
                oReturn.InvokeMethod("GetOwner", DirectCast(o, Object()))
    
                Return o(0)
            Next
        End Function
    
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    
        End Sub
    
        Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
    
        End Sub
    End Class
    
    


    You've taught me everything I know but not everything you know.

    Thursday, December 27, 2012 8:44 AM
  • There's no problem, nor an error. It's just that I'm currently listing the running processes for all users, but I want to list the processes that the current user is running only. I will take a look at the answers, including yours.
    Thursday, December 27, 2012 11:02 AM
  • Rionto,

    Maybe you should first describe what is in your opinion a process. 

    Although the computer is handling more processes,  is a windows system not a multi user OS which has more than one current user active. 

    Beside that are under the account but for this user running administrator and system processes. Those processes can as it is about a server handle services to other computers, but that are not users in the case of current user.

    Be aware that on a Windows Server can run for instance Citrix applications, that are processes which do the processes for more users. But still under the current user account of that (Server) computer. 


    Success
    Cor

    Thursday, December 27, 2012 11:15 AM
  • I'm going to describe it a little bit better:

    So what I have now in my VB application is the code above that I'm using to list processes in a combobox. When I look at the combobox, there are hundreds of processes and I want it stripped down. I've tried the suggestion of bilde2910 and indeed, it gave me a combobox with much less processes, but I think it only got rid of the SYSTEM processes. Now, what I want is actually really simple, it's just to list the processes that the user who has opened my VB application is running. In the picture below, Katrien is the Admin and robbedoes is a user. If robbedoes opened the program, it would list all the processes that are currently runned by robbedoes. If Katrien opens the program, the only processes that would be in the combobox are 'wampmanager.exe' and 'Winject.exe'. I hope I'm clear to you guys...


    • Edited by Rionto Thursday, December 27, 2012 11:57 AM
    Thursday, December 27, 2012 11:41 AM
  • I think that this gets the most where you are, I created it by some searching on Internet.

            Dim searcher As New ManagementObjectSearcher("Select * from Win32_Process")
            Dim processList As ManagementObjectCollection = searcher.Get()
            For Each obj As ManagementObject In processList
                Dim owner = {""}
                Dim returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", owner))
                If returnVal = 0 Then
                    If owner(0) = Environment.UserName Then
                        ListBox1.Items.Add(obj.Item("Name"))
                    End If
                End If
            Next


    Success
    Cor

    • Marked as answer by Rionto Thursday, December 27, 2012 3:18 PM
    Thursday, December 27, 2012 1:07 PM
  • This pretty much does what you want.

    Imports System.Diagnostics
    Imports System.Management    'Add reference Assemblies, Framework, System.Management
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListView1.AllowColumnReorder = True
            ListView1.GridLines = True
            ListView1.View = View.Details
            ListView1.Sorting = SortOrder.Ascending
    
            ListView1.Columns.Add("Process ID", 100, HorizontalAlignment.Left)
            ListView1.Columns.Add("Process Name", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("User Name", 100, HorizontalAlignment.Left)
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim psList() As Process
            Try
                psList = Process.GetProcesses()
    
                For Each p As Process In psList
                    If GetProcessUserName(p) = Environment.UserName Then
                        Dim item1 As New ListViewItem(p.Id.ToString(), 0)
                        item1.SubItems.Add(p.ProcessName)
                        item1.SubItems.Add(Environment.UserName)
                        ListView1.Items.AddRange(New ListViewItem() {item1})
                    End If
                Next p
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Function GetProcessUserName(ByVal Process As Process) As String
            Dim sq As New ObjectQuery("Select * from Win32_Process Where ProcessID = '" & Process.Id & "'")
            Dim searcher As New ManagementObjectSearcher(sq)
    
            If searcher.Get.Count = 0 Then Return Nothing
    
            For Each oReturn As ManagementObject In searcher.Get
                Dim o As String() = New String(1) {}
    
                'Invoke the method and populate the o var with the user name and domain                                           
                oReturn.InvokeMethod("GetOwner", DirectCast(o, Object()))
    
                Return o(0)
            Next
        End Function
    
        Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
    
        End Sub
    
    End Class
    
    


    You've taught me everything I know but not everything you know.

    • Proposed as answer by Cor Ligthert Thursday, December 27, 2012 2:19 PM
    • Marked as answer by Rionto Thursday, December 27, 2012 3:18 PM
    Thursday, December 27, 2012 2:06 PM
  • I think I will get there with your help, but could you just help me evade the following errors? I'm really just a noobie...

    Type 'ManagementObjectSearcher' is not defined.
    Type 'ManagementObjectCollection' is not defined.
    Type 'ManagementObject' is not defined.

    Thanks
    Thursday, December 27, 2012 3:22 PM
  • Add a reference to System.Management (under .NET), and place the following code at the top of your code file (if you have Option statements, place it below those):

    Imports System.Management

    Sincerely yours,
    - bilde2910


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
    Check out my development so far!

    • Edited by bilde2910 Thursday, December 27, 2012 5:48 PM
    Thursday, December 27, 2012 5:48 PM
  • I think I will get there with your help, but could you just help me evade the following errors? I'm really just a noobie...

    Type 'ManagementObjectSearcher' is not defined.
    Type 'ManagementObjectCollection' is not defined.
    Type 'ManagementObject' is not defined.

    Thanks

    Even if you added Imports System.Management at the top of your code you may need to add a reference to System.Management by going into visual studio and under the project dropdown list go to Add Reference, Assemblies, Framework and scroll down till you see System.Management then check it.

    You've taught me everything I know but not everything you know.

    Thursday, December 27, 2012 6:07 PM
  • Is it possible to adjust the string that's being added to the ListBox? I want to change process.exe to process (as a string), by using the replace method. Can you show me how I can do that?
    Wednesday, January 2, 2013 6:29 PM
  • It's simplest to use IO.Path.GetFileNameWithoutExtension(s). You could use String.Replace, but there is a chance the process could be called "someprocess.exe.exe".

    HTH,

    Andrew

    • Marked as answer by Rionto Wednesday, January 2, 2013 7:07 PM
    • Unmarked as answer by Rionto Wednesday, January 2, 2013 7:08 PM
    • Marked as answer by Rionto Wednesday, January 2, 2013 7:10 PM
    • Unmarked as answer by Rionto Wednesday, January 2, 2013 7:10 PM
    Wednesday, January 2, 2013 7:01 PM
  • Please take a look at the code Cor provided me. It's his code I'm using.
    Wednesday, January 2, 2013 7:12 PM
  • OK, so what you need to do is look at Cor's code and figure out where the process name comes from. You then use that value in GetFileNameWithoutExtension() and add the result to the ListBox1.Items collection instead of the raw process name.

    HTH,

    Andrew

    Wednesday, January 2, 2013 7:22 PM
  • Like this:

    If owner(0) = Environment.UserName Then
    	Dim shortName = IO.Path.GetFileNameWithoutExtension(obj.Item("Name"))
    	ListBox1.Items.Add(shortName)
    End If
    

    HTH,

    Andrew

    • Marked as answer by Rionto Wednesday, January 2, 2013 9:11 PM
    Wednesday, January 2, 2013 8:24 PM
  • That has done the job for me. Thanks!

    Wednesday, January 2, 2013 9:11 PM