tags:

views:

2891

answers:

5

The title pretty much says it all. I'm just concerned about Windows, so there's no need to go into esoterica about Mono compatibility or anything like that.

I should also add that the app that I'm writing is WPF, and I'd prefer to avoid taking a dependency on System.Windows.Forms if at all possible.

A: 

The simplest way would be to create an Autoplay Handler:

http://www.codeproject.com/KB/system/AutoplayDemo.aspx

Autoplay Version 2 is a feature in Windows XP that will scan the first four levels of a removable media, when it arrives, looking for media content types (music, graphics, or video). Registration of applications is done on a content type basis. When a removable media arrives, Windows XP determines what actions to perform by evaluating the content and comparing it to registered handlers for that content.

A detailed MSDN article is also available.

amdfan
That's pretty cool, but I'm really just looking for something that will operate while my software is running. Thanks, though.
David Mitchell
+7  A: 

Give this a shot...

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace WMITestConsolApplication
{

    class Program
    {

     static void Main(string[] args)
     {

      AddInsertUSBHandler();
      AddRemoveUSBHandler();
      while (true) {
      }

     }

     static ManagementEventWatcher w = null;

     static void AddRemoveUSBHandler()
     {

      WqlEventQuery q;
      ManagementScope scope = new ManagementScope("root\\CIMV2");
      scope.Options.EnablePrivileges = true;

      try {

       q = new WqlEventQuery();
       q.EventClassName = "__InstanceDeletionEvent";
       q.WithinInterval = new TimeSpan(0, 0, 3);
       q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
       w = new ManagementEventWatcher(scope, q);
       w.EventArrived += USBRemoved;

       w.Start();
      }
      catch (Exception e) {


       Console.WriteLine(e.Message);
       if (w != null)
       {
        w.Stop();

       }
      }

     }

     static void AddInsertUSBHandler()
     {

      WqlEventQuery q;
      ManagementScope scope = new ManagementScope("root\\CIMV2");
      scope.Options.EnablePrivileges = true;

      try {

       q = new WqlEventQuery();
       q.EventClassName = "__InstanceCreationEvent";
       q.WithinInterval = new TimeSpan(0, 0, 3);
       q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
       w = new ManagementEventWatcher(scope, q);
       w.EventArrived += USBInserted;

       w.Start();
      }
      catch (Exception e) {

       Console.WriteLine(e.Message);
       if (w != null)
       {
        w.Stop();

       }
      }

     }

     static void USBInserted(object sender, EventArgs e)
     {

      Console.WriteLine("A USB device inserted");

     }

     static void USBRemoved(object sender, EventArgs e)
     {

      Console.WriteLine("A USB device removed");

     }
    }

}
Josh Stodola
What assembly is the ManagementEventWatcher in?
David Mitchell
System.Management, according to this MSDN article: http://msdn.microsoft.com/en-us/library/system.management.managementeventwatcher.aspx
amdfan
Looks like it is in System.Management.dll - http://msdn.microsoft.com/en-us/library/system.management.managementeventwatcher.aspx
Todd White
@amdfan: Jinx you owe me a coke! ;)
Todd White
@Josh: might I recommend that you rename the "AddInsetUSBHandler" to "AddInsertUSBHandler"?
David Mitchell
@David: I made the necessary edit to correct that.
Jeff Yates
On my system I get 4 insert and 4 remove events when I plug in, then yank out my pen drive...
Alan
+6  A: 

There are much less cumbersome ways of doing this than using WMI polling - just capture WM_DEVICECHANGE:

http://msdn.microsoft.com/en-us/library/aa363215.aspx

Paul Betts
A: 

Hello....... that is very nice. but, I am getting 3 Event arrived and i have just insert one usb device.....why is this.

A: 

Hy, you can check out this guy's code here http://karlagius.com/2009/03/04/keeping-track-of-usb-devices-using-wmi/. The interesting bit is the ForgetMeNot\Controllers\DeviceListController.cs file.

pinocet