<p>I am trying to register to a "Device added/ Device removed" event using WMI. When I say device - I mean something in the lines of a Disk-On-Key or any other device that has files on it which I can access...</p>

<p>I am registering to the event, and the event is raised, but the EventType propery is different from the one I am expecting to see.</p>

<p>The documentation (<a href="http://msdn.microsoft.com/en-us/library/aa394124(VS.85).aspx" rel="nofollow noreferrer">MSDN</a>) states : 1- config change, 2- Device added, 3-Device removed 4- Docking. For some reason I always get a value of 1. </p>

<p>Any ideas ?</p>

<p>Here's sample code : </p>

<pre><code>public class WMIReceiveEvent
{
    public WMIReceiveEvent()
    {
        try
        {
            WqlEventQuery query = new WqlEventQuery(
                "SELECT * FROM Win32_DeviceChangeEvent");

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

            watcher.EventArrived += 
                new EventArrivedEventHandler(
                HandleEvent);

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

            // Do something while waiting for events
            System.Threading.Thread.Sleep(10000);

            // Stop listening for events
            watcher.Stop();
            return;
        }
        catch(ManagementException err)
        {
            MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
        }
    }

    private void HandleEvent(object sender,
        EventArrivedEventArgs e)
    {
        Console.WriteLine(e.NewEvent.GetPropertyValue["EventType"]);
    }

    public static void Main()
    {
        WMIReceiveEvent receiveEvent = new WMIReceiveEvent();
        return;
    }
}
</code></pre>

[{'answer_id': 40706, 'author': 'dguaraglia', 'author_id': 2384, 'author_profile': 'https://Stackoverflow.com/users/2384', 'pm_score': 0, 'selected': False, 'text': "<p>Oh! Yup, I've been through that, but using the raw Windows API calls some time ago, while developing an ActiveX control that detected the insertion of any kind of media. I'll try to unearth the code from my backups and see if I can tell you how I solved it. I'll subscribe to the RSS just in case somebody gets there first.</p>\n"}, {'answer_id': 62199, 'author': 'dguaraglia', 'author_id': 2384, 'author_profile': 'https://Stackoverflow.com/users/2384', 'pm_score': 1, 'selected': False, 'text': '<p>Well, I couldn\'t find the code. Tried on my old RAC account, nothing. Nothing in my old backups. Go figure. But I tried to work out how I did it, and I think this is the correct sequence (I based a lot of it on this <a href="http://www.codeproject.com/KB/system/HwDetect.aspx" rel="nofollow noreferrer">article</a>):</p>\n\n<ol>\n<li>Get all drive letters and cache\nthem. </li>\n<li>Wait for the WM_DEVICECHANGE\nmessage, and start a timer with a\ntimeout of 1 second (this is done to\navoid a lot of spurious\nWM_DEVICECHANGE messages that start\nas start as soon as you insert the\nUSB key/other device and only end\nwhen the drive is "settled").</li>\n<li>Compare the drive letters with the\nold cache and detect the new ones.</li>\n<li>Get device information for those.</li>\n</ol>\n\n<p>I know there are other methods, but that proved to be the only one that would work consistently in different versions of windows, and we needed that as my client used the ActiveX control on a webpage that uploaded images from any kind of device you inserted (I think they produced some kind of printing kiosk).</p>\n'}, {'answer_id': 1120409, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Well,</p>\n\n<p>u can try win32_logical disk class and bind it to the __Instancecreationevent.\nYou can easily get the required info</p>\n'}, {'answer_id': 2398988, 'author': 'JoelHess', 'author_id': 14509, 'author_profile': 'https://Stackoverflow.com/users/14509', 'pm_score': 0, 'selected': False, 'text': '<p>I tried this on my system and I eventually get the right code. It just takes a while. I get a dozen or so events, and one of them is the device connect code.</p>\n'}]
['https://Stackoverflow.com/questions/39704', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']