我知道如何使用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#回答
从processID
获取流程路径怎么样。?就像
System.Diagnostics.Process.GetProcessById(processID).MainModule.FileName;
这里有一些关于MainModule
进程的信息。主模块属性
我不知道为什么在这里你从string
转换成int
,然后从int
转换成string
Dim processID As String = Convert.ToInt32(e.NewEvent.Properties("ProcessID").Value).ToString()