Bruce
Bruce

Reputation: 11

Get RemovableMedia devices info using C#

I want to get detailed info about all removable media devices such as USB disk,Smartcard reader etc. and integrated devices such as Fingerprint reader,integrated camera etc on my Windows system using C#. If it can be done through WMI then please tell me detailded approach as I've tried querying "Win32_LogicalDisk" class but that didn't yield much helpful results. Also there is windows API "DeviceIOControl",can it help me much in this regard?

Upvotes: 1

Views: 710

Answers (1)

seshuk
seshuk

Reputation: 182

Did you check the below class?

  System.IO.DriveInfo

What detailed information are you looking for in addition to the ones provided by this class?

http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx

You can check other classes in this namespace.

Edit based on the comment:

If you want to detect the device at runtime, you may use:

ManagementEventWatcher & WqlEventQuery

Below is an example:

WqlEventQuery _q = new WqlEventQuery("__InstanceOperationEvent", "TargetInstance ISA    'Win32_USBControllerDevice' ");
        _q.WithinInterval = TimeSpan.FromSeconds(1);
        _usbWatcher = new ManagementEventWatcher(_q);
        _usbWatcher.EventArrived += new   EventArrivedEventHandler(OnUSBDetectedOrDetached);
        _usbWatcher.Start();

and in the event handler:

void OnUSBDetectedOrDetached(object sender, EventArrivedEventArgs e)
    {
        ManagementBaseObject _o = e.NewEvent["TargetInstance"] as ManagementBaseObject;
        if (_o != null)
        {
            using (ManagementObject mo = new ManagementObject(_o["Dependent"].ToString()))
            {
                if (mo != null)
                {
                    try
                    {
                        string devId = string.Empty;
                        devId = mo.GetPropertyValue("DeviceID").ToString();
                        if (devId != string.Empty)
                        {
                            Trace.WriteLine("Detected USB Device, DevId: " + devId);
                        }
....

Upvotes: 1

Related Questions