tags:

views:

238

answers:

2

I am working on a project in which I need to be able to detect when a CD or a USB drive is inserted or removed. I found some source code that was supposed to do this very thing, however, nothing seems to happen when I insert or eject a CD. Could someone please verify that the source is correct, and give me any pointers as to what I may have done wrong here?

public class MyWindow
{
    ManagementEventWatcher w;

    private void MyWindow_Loaded(object sender, RoutedEventArgs e)
    {
        WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 2");
        ConnectionOptions opt = new ConnectionOptions();
        opt.EnablePrivileges = true;

        ManagementScope ms = new ManagementScope("root\\CIMV2", opt);

        w = new ManagementEventWatcher(ms, query);

        w.EventArrived += new EventArrivedEventHandler(w_EventArrived);
        w.Start();
    }

    private void w_EventArrived(object sender, EventArrivedEventArgs e)
    {
        PropertyData pd = e.NewEvent.Properties["TargetInstance"];
    }
}

When I set a breakpoint on the "PropertyData pd = ..." line, it never gets hit when I eject/insert a CD. Since I've not messed with this at all, and all of the examples I've seen online simply cite this same source code (with minor variations) I'm coming to you guys for help. Thanks for any advice you can offer!

~md5sum~

+1  A: 

public void networkDevice() { try { WqlEventQuery q = new WqlEventQuery(); q.EventClassName = "__InstanceModificationEvent"; q.WithinInterval = new TimeSpan(0, 0, 1); q.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5";

            ConnectionOptions opt = new ConnectionOptions();
            opt.EnablePrivileges = true;
            opt.Authority = null;
            opt.Authentication = AuthenticationLevel.Default;
            //opt.Username = "Administrator";
            //opt.Password = "";
            ManagementScope scope = new ManagementScope("\\root\\CIMV2", opt);

            ManagementEventWatcher watcher = new ManagementEventWatcher(scope, q);
            watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
            watcher.Start();
        }
        catch (ManagementException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    void watcher_EventArrived(object sender, EventArrivedEventArgs e)
    {
        ManagementBaseObject wmiDevice = (ManagementBaseObject)e.NewEvent["TargetInstance"];
        string driveName = (string)wmiDevice["DeviceID"];
        Console.WriteLine(driveName);
        Console.WriteLine(wmiDevice.Properties["VolumeName"].Value);
        Console.WriteLine((string)wmiDevice["Name"]);
        if (wmiDevice.Properties["VolumeName"].Value != null)
            Console.WriteLine("CD has been inserted");
        else
            Console.WriteLine("CD has been ejected");
    }
bayangali
Thanks! That worked... I'm a little confused as to why though. I had tried `...DriveType = 5` too, and the only other difference I see is the `\\` before `root\\CIMV2`. PEBCAK mebbe? Only other thing I ask is that the formatting on your answer be fixed so it's readable. Thanks again so very much!
md5sum
A: 

Is this code is able to detect USB (flash) and CD/DVD drives? What does TargetInstance.DriveType = 5 means?

Laserson
This should be a comment on (at a minimum of) the answer provided. And the answer to your question is "Yes" it will detect CD/DVD and USB attached storage. The `TargetInstance.DriveType` is the type of the drive to be detected, where the `= 5` sets it to "Removable Media"
md5sum