Results 1 to 3 of 3

Thread: How to check if process is running by path

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2020
    Posts
    5

    Question How to check if process is running by path

    How do I check if a process is running, by path?

    I have an application which displays in a listbox a list of files(including the full path). How do I select all items from the listbox and terminate their processes by path ? (NOT by filename only)

    I have the below code , which triggers an error and the application stops immediately: I have a button which the user clicks and triggers an timer event. I need to use a timer, because of the many files the listbox will have. (the items which will be terminated)

    Timer code:

    Code:
    For Each k In Listbox2.Items
            Dim path As String = k
            Dim matchingProcesses = New List(Of Process)
    
            For Each process As Process In Process.GetProcesses()
                For Each m As ProcessModule In process.Modules
                    If String.Compare(m.FileName, Path, StringComparison.InvariantCultureIgnoreCase) = 0 Then
                        matchingProcesses.Add(process)
                        Exit For
                    End If
                Next
            Next
    
            For Each p As Process In matchingProcesses
                p.Kill()
            Next
    Thanks in advance

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to check if process is running by path

    You would call GetProcessesByName to filter by file name, then test the MainModule.FileName property to confirm the file path. Something like this:
    vb.net Code:
    1. For Each proc In Process.GetProcessesByName(fileName).Where(Function(p) p.MainModule.FileName = filePath).ToArray()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: How to check if process is running by path

    Trouble with Mainmoduel is it uses the access token PROCESS_QUERY_INFORMATION. Any elevated process will trow win32exception.

    vb Code:
    1. Namespace Classes.Processes
    2.  
    3.  
    4.     ''' <summary>
    5.     '''     <copyright file="ProcessPath.vb" company="Simple Coders">
    6.     '''          Copyright (c) Simple Coders. All rights reserved.  
    7.     '''     </copyright>                                                                
    8.     ''' </summary>
    9.     Friend Class ProcessPath
    10.  
    11.  
    12.         ''' <summary>
    13.         '''     Retrieves the full name of the executable image for the specified process.
    14.         ''' </summary>
    15.         ''' <param name="id">
    16.         '''      The unique identifier for the associated process.
    17.         ''' </param>
    18.         ''' <returns>
    19.         '''     If the function succeeds, the return value is the length of the string that is copied to the buffer, in characters,
    20.         '''     not including the terminating null character. If the buffer is too small to hold the module name, the string is truncated to
    21.         '''     nSize characters including the terminating null character, the function returns nSize, and the function sets the last error to
    22.         '''     ERROR_INSUFFICIENT_BUFFER.
    23.         ''' </returns>
    24.         ''' <remarks>
    25.         '''     See [url]https://msdn.microsoft.com/en-us/library/windows/desktop/ms684919%28v=vs.85%29.aspx[/url]
    26.         ''' </remarks>
    27.         Friend  Function GetProcessPath(id As Integer) As String
    28.             Dim path As String = String.Empty
    29.             Dim buffer As New StringBuilder(1024)
    30.             Dim handle As IntPtr = NativeMethods.OpenProcess(QueryLimitedInformation, False, id)
    31.  
    32.  
    33.             Try
    34.                 If handle <> NativeMethods.NullHandleValue Then
    35.                     Dim bSize As Integer = buffer.Capacity
    36.                     If NativeMethods.QueryFullProcessImageName(handle, 0, buffer, bSize) Then
    37.                         path = buffer.ToString()
    38.                     End If
    39.                 End If
    40.             Finally
    41.                 If handle <> NativeMethods.NullHandleValue Then
    42.                     NativeMethods.CloseHandle(handle)
    43.                 End If
    44.  
    45.  
    46.                 buffer.Clear()
    47.             End Try
    48.  
    49.  
    50.             Return path
    51.         End Function
    52.     End Class
    53. End Namespace
    My Github - 1d3nt

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width