/// <summary>
        /// Initializes a new instance of the <see cref="WmiChangeEventTester"/> class.
        /// </summary>
        public WmiChangeEventTester()
        {
            try
            {
                // Your query goes below; "KeyPath" is the key in the registry that you
                // want to monitor for changes. Make sure you escape the \ character.
                System.Management.WqlEventQuery query = new System.Management.WqlEventQuery(
                    @"SELECT * FROM RegistryValueChangeEvent 
WHERE Hive = 'HKEY_LOCAL_MACHINE' 
AND KeyPath = 'SOFTWARE\\Microsoft\\.NETFramework' 
AND ValueName = 'InstallRoot' 
");

                System.Management.ManagementEventWatcher watcher = new System.Management.ManagementEventWatcher(query);
                System.Console.WriteLine("Waiting for an event...");

                // Set up the delegate that will handle the change event.
                watcher.EventArrived += new System.Management.EventArrivedEventHandler(HandleEvent);

                // Start listening for events.
                watcher.Start();

                // Do something while waiting for events. In your application,
                // this would just be continuing business as usual.
                System.Threading.Thread.Sleep(100000000);

                // Stop listening for events.
                watcher.Stop();
            }
            catch (System.Management.ManagementException managementException)
            {
                System.Console.WriteLine("An error occurred: " + managementException.Message);
            }
        }
Exemplo n.º 2
0
        public void DeviceRemovedHandler(object sender, System.Management.EventArrivedEventArgs e)
        {
            System.Management.ManagementEventWatcher removal = (System.Management.ManagementEventWatcher)sender;

            var targetInstanceData   = e.NewEvent.Properties["TargetInstance"];
            var targetInstanceObject = (System.Management.ManagementBaseObject)targetInstanceData.Value;

            foreach (var prop in targetInstanceObject.Properties)
            {
                if (prop.Name == "PNPDeviceID")
                {
                    var pnpDeviceId = targetInstanceObject["PNPDeviceID"].ToString();

                    if (pnpDeviceId.Contains("USB\\VID_0483&PID_5740"))
                    {
                        var deviceId = targetInstanceObject["DeviceID"].ToString();
                        if (portExtender_.CheckDeviceRemoved())
                        {
                            removal.Stop();
                            portExtender_.CloseDevice();

                            DeviceRemovedStatusUpdate();
                        }
                    }
                    return;
                }
            }

            removal.WaitForNextEvent();
        }
Exemplo n.º 3
0
        public Form1()
        {
            InitializeComponent();
            mgmtWtch = new System.Management.ManagementEventWatcher("Select * From Win32_ProcessStartTrace");
            mgmtWtch.EventArrived += new System.Management.EventArrivedEventHandler(CheckingBlackListEvent);

            mgmtWtch.Start();
        }