我试图使用eventwathcer,但它没有使用11个字符的进程名称(AbcdEfghIII.exe)。如果我写10个字符的进程名称(AbcdEfgIII.exe)就可以了。有人格限制还是我做错了什么?这是我的代码:
Imports System.Management
Public Class Form1
Dim WithEvents StopWatch As New ManagementEventWatcher(New WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"))
Private Sub StopWatch_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs)
If e.NewEvent.Properties("ProcessName").Value.ToString = "AbcdEfghIII.exe" Then
MsgBox("Closed")
End If
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
StopWatch.Stop()
RemoveHandler StopWatch.EventArrived, AddressOf StopWatch_EventArrived
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
AddHandler StopWatch.EventArrived, AddressOf StopWatch_EventArrived
StopWatch.Start()
Catch g As Exception
MsgBox("Please, run as admin.", MsgBoxStyle.Critical, "Error")
Me.Close()
End Try
End Sub
End Class
答案 0 :(得分:0)
由于似乎没有一种方法可以使其正常工作,我认为您只需要使用hacky解决方法:检查进程名称是否以Customization
开头。< / p>
这样做当然会让它对CustomizationWHATEVER.exe
之类的流程做出反应,但由于您因某些原因而使用的代码并未返回全名,因此如果您需要,则没有其他方式它起作用。
使用您的初始代码:
If check.StartsWith("Customization", StringComparison.OrdinalIgnoreCase) Then
MessageBox.Show("Closed")
End If
以前的答案:
这是一种解决方法:
获取
ProcessID
字段而不是ProcessName
,并将其与Process.GetProcessById()
一起使用以获取.NET友好Process
class实例(您可以从中提取名称)Private Sub StopWatch_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Dim PID As Integer = CType(CType(e.NewEvent.Properties("ProcessID").Value, UInteger) And Integer.MaxValue, Integer) Dim p As Process = Process.GetProcessById(PID) 'NOTE: The ".exe" part is excluded in Process.ProcessName. If String.Equals(p.ProcessName, "Customization", StringComparison.OrdinalIgnoreCase) Then MessageBox.Show("Closed") End If End Sub