Extract file name and path from a running process

I am writing a screen capture application for a client. Part of the capture is fine, but he wants to get the name and path to the file where the capture is located.

Using system.diagnostics.process I can get the process in which the capture is performed, and can get the full path to the EXE, but not the file that is open.

t. Notepad is open with TextFile1.txt as a document. I can get from the MainWindowTitle process which will be "TextFile1.txt - Notepad", but what I need is more like "c: \ users .... \ TextFile1.txt"

Is there a way to get more information from the process?

I am sure there is a way, but I cannot figure it out

Any help is greatly appreciated.

+5
source share
2

ManagementObjectSearcher, , . , .

Imports System
Imports System.ComponentModel
Imports System.Management
Module Module1
    Sub Main()
        Dim cl() As String
        For Each p As Process In Process.GetProcessesByName("notepad")
            Try
                Using searcher As New ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " & p.Id)
                    For Each mgmtObj As ManagementObject In searcher.Get()
                        cl = mgmtObj.Item("CommandLine").ToString().Split("""")
                        Console.WriteLine(cl(cl.Length - 1))
                    Next
                End Using
            Catch ex As Win32Exception
                'handle error
            End Try
        Next
        System.Threading.Thread.Sleep(1000000)
    End Sub
End Module

DLL:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Managment.dll
+3

,

For Each prog As Process In Process.GetProcesses
    If prog.ProcessName = "notepad" Then
          ListBox1.Items.Add(prog.ProcessName)
     End If
Next
+1

All Articles