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
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:
For Each proc In Process.GetProcessesByName(fileName).Where(Function(p) p.MainModule.FileName = filePath).ToArray()
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:
Namespace Classes.Processes
''' <summary>
''' <copyright file="ProcessPath.vb" company="Simple Coders">
''' Copyright (c) Simple Coders. All rights reserved.
''' </copyright>
''' </summary>
Friend Class ProcessPath
''' <summary>
''' Retrieves the full name of the executable image for the specified process.
''' </summary>
''' <param name="id">
''' The unique identifier for the associated process.
''' </param>
''' <returns>
''' If the function succeeds, the return value is the length of the string that is copied to the buffer, in characters,
''' not including the terminating null character. If the buffer is too small to hold the module name, the string is truncated to
''' nSize characters including the terminating null character, the function returns nSize, and the function sets the last error to
''' ERROR_INSUFFICIENT_BUFFER.
''' </returns>
''' <remarks>
''' See [url]https://msdn.microsoft.com/en-us/library/windows/desktop/ms684919%28v=vs.85%29.aspx[/url]
''' </remarks>
Friend Function GetProcessPath(id As Integer) As String
Dim path As String = String.Empty
Dim buffer As New StringBuilder(1024)
Dim handle As IntPtr = NativeMethods.OpenProcess(QueryLimitedInformation, False, id)
Try
If handle <> NativeMethods.NullHandleValue Then
Dim bSize As Integer = buffer.Capacity
If NativeMethods.QueryFullProcessImageName(handle, 0, buffer, bSize) Then
path = buffer.ToString()
End If
End If
Finally
If handle <> NativeMethods.NullHandleValue Then
NativeMethods.CloseHandle(handle)
End If
buffer.Clear()
End Try
Return path
End Function
End Class
End Namespace