WMI query on VolumeChangeEvent for SD card insertion into SD card reader not working

Viewed 688

I'm developing an application where I would like to process data automatically from an SD card when it's inserted in an SD card reader. The SD card reader is inserted into the computer and assigned the E: and F: drives and are greyed out when there's no SD card inserted. I've been trying to use an WMI query based on Win32__VolumeChangeEvent or Win32__DeviceChangeEvent, but without success. The error I get for "select * from Win32_VolumeChangeEvent" is invalid class, and "select * from Win32_DeviceChangeEvent" doesn't show me status updates when I insert an SD card into the SD card reader, only for the insertion and removal of the SD card reader itself.

A class that I've been using to experiment is :

 namespace eventTest
{
    class WMIReceiveEvent
    {
        static void Main(string[] args)
        {
            WMIReceiveEvent receiveEvent = new WMIReceiveEvent();
            Console.Read();
        }

        public WMIReceiveEvent()
        {
            try
            {
                ManagementScope scope = new ManagementScope("root\\CIMV2");
                scope.Options.EnablePrivileges = true;
                WqlEventQuery query = new WqlEventQuery("select * from Win32_DeviceChangeEvent");
                ManagementEventWatcher watcher = new ManagementEventWatcher(scope, query);
                Console.WriteLine("Waiting for an event...");

                watcher.EventArrived += new EventArrivedEventHandler(HandleEvent);

                // Start listening for events
                watcher.Start();
            }
            catch (ManagementException err)
            {
                Console.WriteLine("An error occurred while trying to receive an event: " + err.Message);
            }
        }

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

Can anyone provide we with a query that I can use to monitor the arrival of the SD card in Windows or point me out why Win32_VolumeChangeEvent isn't accepted as a valid class and if fixed how it can be used to monitor the arrival of the actual SD card?

1 Answers
Related