public class ProcessWatcher : IDisposable
Public Class ProcessWatcher
Implements IDisposable
Dim instance As ProcessWatcher
public ref class ProcessWatcher : IDisposable
type ProcessWatcher =
class
interface IDisposable
end
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
Imports System.Management
Public Class Form1 : Inherits Form
Private WithEvents processWatcher As New ProcessWatcher
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles Me.Load
Me.processWatcher.Start()
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) _
Handles Me.FormClosing
Me.processWatcher.Dispose()
End Sub
Private Sub ProcessWatcher_ProcessStarted(ByVal sender As Object, ByVal e As EventArrivedEventArgs) _
Handles processWatcher.ProcessStarted
Dim name As String = CStr(e.NewEvent.Properties("ProcessName").Value)
Dim pid As Integer = CInt(e.NewEvent.Properties("ProcessId").Value)
Dim path As String = String.Empty
Dim connOptions As New ConnectionOptions With {
.Impersonation = ImpersonationLevel.Impersonate,
.EnablePrivileges = True
}
Dim scope As New ManagementScope(String.Format("\\{0}\ROOT\CIMV2", Environment.MachineName), connOptions)
scope.Connect()
Dim query As New SelectQuery(String.Format("SELECT * FROM Win32_Process WHERE ProcessId={0}", pid))
Dim enumOptions As New EnumerationOptions With {.ReturnImmediately = True}
Using mos As New ManagementObjectSearcher(scope, query, enumOptions),
moc As ManagementObjectCollection = mos.Get(),
mo As ManagementObject = DirectCast(moc(0), ManagementObject)
path = CStr(mo.Properties("ExecutablePath").Value)
End Using
Console.WriteLine(String.Format("Process started | Name: {0}", name))
Console.WriteLine(String.Format("Process started | PID : {0}", pid))
Console.WriteLine(String.Format("Process started | Path: {0}", path))
End Sub
Private Sub ProcessWatcher_ProcessStopped(ByVal sender As Object, ByVal e As EventArrivedEventArgs) _
Handles processWatcher.ProcessStopped
Dim name As String = e.NewEvent.Properties("ProcessName").Value.ToString()
Console.WriteLine(String.Format("Process stopped | Name: {0}", name))
End Sub
End Class
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
ProcessWatcher | Initializes a new instance of the ProcessWatcher class. |
Dispose | Releases all the resources used by this instance. |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
GetHashCode | Serves as the default hash function. (Inherited from Object) |
GetType | Gets the Type of the current instance. (Inherited from Object) |
Start | Start monitoring for process starts and stops. |
Stop | Stop monitoring for process starts and stops. |
ToString | Returns a string that represents the current object. (Inherited from Object) |
ProcessStarted | Occurs when a process starts (run). |
ProcessStopped | Occurs when a process stops (exit). |
CanConvertTo |
Determines whether the source object can be converted to the specified target type.
(Defined by ObjectExtensions) |
CanConvertToT |
Determines whether the source object can be converted to the specified target type.
(Defined by ObjectExtensions) |
ConvertToT |
Converts an object to the specified target type.
If the conversion fails, an exception is thrown.
(Defined by ObjectExtensions) |
ConvertToT |
Converts an object to the specified target type.
If the conversion fails, returns the specified default value.
(Defined by ObjectExtensions) |
IsDisposable |
Determines whether the specified object is a disposable type
(i.e., it implements IDisposable interface).
(Defined by ObjectExtensions) |
Speak |
Speaks the string representation of the source object by using the
operating system integrated text-to-speech synthesizer.
(Defined by ObjectExtensions) |
Speak |
Speaks the string representation of the source object by using the
operating system integrated text-to-speech synthesizer.
(Defined by ObjectExtensions) |
ThrowIfNullTException |
Throws the specified exception if the source object is null.
(Defined by ObjectExtensions) |