2017-11-29 1 views
-1

eventwathcer를 사용하려고하는데 11 문자 프로세스 이름 (AbcdEfghIII.exe)으로 작동하지 않습니다. 나는 10 문자의 프로세스 이름 (AbcdEfgIII.exe)을 쓰면된다. 글자 수 제한이 있습니까? 아니면 제가 잘못하고 있습니까? 여기 내 코드입니다 :ManagementEventWatcher 글자 수 제한이 있습니까?

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

_ "작동하지 않음"_을 정의하십시오. 오류가 발생합니까? 또한 e.NewEvent.Properties ("ProcessName"). Value.ToString()'을 변수에 넣은 다음 [** breakpoint **] (https://msdn.microsoft.com/en-us/)를 입력하십시오. 라이브러리/5557y8b4.aspx)를 다음 행에 추가하십시오. 중단 점에 도달하면 마우스로 변수 위에 마우스를 올려 해당 값을 검사합니다. 그렇게하면 프로세스의 이름이 실제로 예상 한 이름인지 확인할 수 있습니다. –

+0

지금 해결되었습니다. 변수를 사용하여, 고마워요. –

+0

@VisualVincent 해결되었지만 다시 작동하지 않는다고 생각했습니다. 'Dim check = e.NewEvent.Properties ("ProcessName") .Value.ToString Dim check1 = "AbcdEfghIII.exe" check = check1 Then MsgBox ("닫혔다") End If' 그리고 나는 그 공중 선회를 이해하지 못했습니다. –

답변

0

제대로이 일을 할 수있는 방법이있을 것 같지 않기 때문에, 나는 당신이 단지 해키 해결 방법을 사용해야합니다 가정 : 프로세스 이름이 Customization로 시작하면 확인을.

그렇다면 물론 CustomizationWHATEVER.exe과 같은 프로세스에 반응하지만, 어떤 이유로 사용중인 코드가 전체 이름을 반환하지 않으므로 작동하려면 다른 방법이 없습니다.

초기 코드를 사용 :

If check.StartsWith("Customization", StringComparison.OrdinalIgnoreCase) Then 
    MessageBox.Show("Closed") 
End If 

PREVIOUS 답변 : 여기

는 해결 방법입니다 :

대신 ProcessNameProcessID 필드 가져 오기를 사용하는 것이 Process.GetProcessById()와 함께 .NE를 얻습니다. T 친한 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 
+0

다시는 아무 일도 없었습니다 :/또한 이전처럼 ("Customiza")와 같이 짧은 이름으로 시도했지만 작동하지도 않았습니다. 이봐 요, 이상한 일이 ... –

+0

@ErokayYurdaer : 글자 제한이 없다고 말했듯이, 줄을 줄이는 것은 효과가 없습니다. 'If' 밖에있는 메시지 상자에 표시하면 무엇을 얻게 될까요? MessageBox.Show (p.ProcessName)? (나는'Customization'을 보여줄 때만 관심이있다.) –

+0

No msgbox가 나타난다 –

관련 문제