0

我知道如何使用 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# 来回答。

4

1 回答 1

0

那么从processID.获取进程路径呢?像那样

System.Diagnostics.Process.GetProcessById(processID).MainModule.FileName;

这里有一些关于MainModule Process.MainModule 属性的信息

而且我不知道为什么你在这里转换从stringint然后从intstring

Dim processID As String = Convert.ToInt32(e.NewEvent.Properties("ProcessID").Value).ToString()
于 2019-11-14T07:39:25.523 回答