我知道如何使用 ManagementEventWatcher 获取 ProcessID 和 ProcessName。我的代码运行良好。
我需要的?
我想得到一个过程的路径。
我想打印 ProcessID、ProcessName 和 ProcessPath。
我的代码:
Dim processStartEvent As ManagementEventWatcher = New ManagementEventWatcher("SELECT * FROM Win32_ProcessStartTrace")
Dim processStopEvent As ManagementEventWatcher = New ManagementEventWatcher("SELECT * FROM Win32_ProcessStopTrace")
Public Sub New()
InitializeComponent()
AddHandler processStartEvent.EventArrived, AddressOf Me.processStartEvent_EventArrived
processStartEvent.Start()
AddHandler processStopEvent.EventArrived, AddressOf Me.processStopEvent_EventArrived
processStopEvent.Start()
End Sub
Private Sub processStartEvent_EventArrived(EventArrivedEventArgs, e)
Dim processName As String = e.NewEvent.Properties("ProcessName").Value.ToString
Dim processID As String = Convert.ToInt32(e.NewEvent.Properties("ProcessID").Value).ToString
FileIO.WriteToFile("Process ID: " & processID & vbNewLine)
FileIO.WriteToFile("Process Name: " & processName & vbNewLine)
End Sub
Private Sub processStopEvent_EventArrived(EventArrivedEventArgs, e)
Dim processName As String = e.NewEvent.Properties("ProcessName").Value.ToString
Dim processID As String = Convert.ToInt32(e.NewEvent.Properties("ProcessID").Value).ToString
FileIO.WriteToFile("Process ID: " & processID & vbNewLine)
FileIO.WriteToFile("Process Name: " & processName & vbNewLine)
End Sub
如果你不熟悉 VB.net,你也可以用 C# 来回答。