.net - Detect when drive is mounted or changes state (WM_DEVICECHANGE for WPF)? -
i writing directory selector control wpf, , add/remove drive directory tree when gets mounted or unmounted or when becomes ready or not ready (e.g. user inserts or removes cd). looking system event similar wm_devicechange.
konstantin
i used wmi implement (like richard stated in answer)
using system.management;  using system;  ...  private void subscribetocdinsertion() {     wqleventquery q;     managementoperationobserver observer = new managementoperationobserver();      // bind local machine     connectionoptions opt = new connectionoptions();     opt.enableprivileges = true; //sets required privilege     managementscope scope = new managementscope("root\\cimv2", opt);      q = new wqleventquery();     q.eventclassname = "__instancemodificationevent";     q.withininterval = new timespan(0, 0, 1);     // drivetype - 5: cdrom     q.condition = @"targetinstance isa 'win32_logicaldisk' , targetinstance.drivetype = 5";     var w = new managementeventwatcher(scope, q);     try     {         // register async. event handler        w.eventarrived += new eventarrivedeventhandler(driveinsertevent);        w.start();      }     catch (exception e)     {         w.stop();     }  }  void driveinsertevent(object sender, eventarrivedeventargs e) {     // event object , display     propertydata pd = e.newevent.properties["targetinstance"];      if (pd != null)     {         managementbaseobject mbo = pd.value managementbaseobject;         // if cd removed volumename == null         if (mbo.properties["volumename"].value != null)         {             //do         }     } }   edit: didn't invent code myself, think got here
Comments
Post a Comment