2013-02-07 39 views
5

我正在爲客戶端編寫屏幕捕獲應用程序。捕獲部分很好,但他想獲取捕獲文件的名稱和路徑。從正在運行的進程中提取文件名和路徑

使用system.diagnostics.process我能夠獲得捕獲的過程,並且可以獲取EXE的完整路徑,但不能獲得打開的文件。

即。記事本是以'TextFile1.txt'作爲文檔打開的。我可以從這個過程中得到MainWindowTitle,它將是'TextFile1.txt - 記事本',但我需要的更像是'c:\ users .... \ TextFile1.txt'

有沒有一種獲得更多來自過程的信息?

我敢肯定有一種方法,但我不能弄清楚

任何幫助極大的讚賞。

回答

3

您可以使用ManagementObjectSearcher來獲取進程的命令行參數,並且在此記事本示例中,可以解析出文件名。下面是寫出來的完整路徑,並在記事本中所有打開的文件的文件名,我不得不引用到這個特定DLL一個簡單的控制檯應用程序例子..

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 

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

感謝您的答覆,我會盡快測試並報告回來。 –

+0

這是非常好的,正是我在找的東西。非常感謝你。 –

1

我認爲這是最簡單的方法

For Each prog As Process In Process.GetProcesses 
    If prog.ProcessName = "notepad" Then 
      ListBox1.Items.Add(prog.ProcessName) 
    End If 
Next 
相關問題