/*-----------------------------------------------------------------------*/ #endregion #region Monitoring /*-----------------------------------------------------------------------*/ /// <summary>Starts the monitoring of device.</summary> /// <param name="path"></param> /// <param name="mediaEvent"></param> public void Monitor(string path, MediaEvent mediaEvent) { if (null == mediaEvent) { throw new ArgumentException("Media event cannot be null!"); } //In case same class was called make sure only one instance is running ///////////////////////////////////////////////////////////// this.Exit(); //Keep logica drive to check ///////////////////////////////////////////////////////////// this.m_logicalDrive = this.GetLogicalDrive(path); WqlEventQuery wql; ManagementOperationObserver observer = new ManagementOperationObserver(); //Bind to local machine ///////////////////////////////////////////////////////////// ConnectionOptions opt = new ConnectionOptions(); //Sets required privilege ///////////////////////////////////////////////////////////// opt.EnablePrivileges = true; ManagementScope scope = new ManagementScope("root\\CIMV2", opt); try { wql = new WqlEventQuery(); wql.EventClassName = "__InstanceModificationEvent"; wql.WithinInterval = new TimeSpan(0, 0, 1); wql.Condition = String.Format(@"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DeviceId = '{0}'", this.m_logicalDrive); this.m_managementEventWatcher = new ManagementEventWatcher(scope, wql); //Register async. event handler ///////////////////////////////////////////////////////////// this.m_managementEventWatcher.EventArrived += new EventArrivedEventHandler(mediaEvent.MediaEventArrived); this.m_managementEventWatcher.Start(); } catch (Exception e) { this.Exit(); throw new Exception("Media Check: " + e.Message); } }
/*-----------------------------------------------------------------------*/ /// <summary>Starts the monitoring of device.</summary> /// <param name="path"></param> /// <param name="mediaEvent"></param> public void Monitor( string path, MediaEvent mediaEvent ) { if( null == mediaEvent ) { throw new ArgumentException( "Media event cannot be null!" ); } //In case same class was called make sure only one instance is running ///////////////////////////////////////////////////////////// this.Exit(); //Keep logica drive to check ///////////////////////////////////////////////////////////// this.m_logicalDrive = this.GetLogicalDrive( path ); WqlEventQuery wql; ManagementOperationObserver observer = new ManagementOperationObserver(); //Bind to local machine ///////////////////////////////////////////////////////////// ConnectionOptions opt = new ConnectionOptions(); //Sets required privilege ///////////////////////////////////////////////////////////// opt.EnablePrivileges = true; ManagementScope scope = new ManagementScope( "root\\CIMV2", opt ); try { wql = new WqlEventQuery(); wql.EventClassName = "__InstanceModificationEvent"; wql.WithinInterval = new TimeSpan( 0, 0, 1 ); wql.Condition = String.Format( @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DeviceId = '{0}'", this.m_logicalDrive ); this.m_managementEventWatcher = new ManagementEventWatcher( scope, wql ); //Register async. event handler ///////////////////////////////////////////////////////////// this.m_managementEventWatcher.EventArrived += new EventArrivedEventHandler( mediaEvent.MediaEventArrived ); this.m_managementEventWatcher.Start(); } catch( Exception e ) { this.Exit(); throw new Exception( "Media Check: " + e.Message ); } }