C#: 监测 USB plugin and plugout

来源:互联网 发布:哈喽聊天软件 编辑:程序博客网 时间:2024/06/07 03:18

It is useful to enum USB ports or connect to a USB device automatically while a USB device is plugged-in. I did lots of research to figure out how to make it work. Hope this can help you.  

                                public void MonitorPlugout()

                                {

                                                m_DataFlow.OnClose += OnDataFlowClose;

                                                WqlEventQuery q;

                                                ManagementScope scope = new ManagementScope("root//CIMV2");

                                                scope.Options.EnablePrivileges = true;

                                                try {

                                                                q = new WqlEventQuery();

                                                                q.EventClassName = "__InstanceDeletionEvent"; // "__InstanceOperationEvent";

                                                                q.WithinInterval = new TimeSpan(0, 0, 3);

                                                                q.Condition = @"TargetInstance ISA 'Win32_USBControllerDevice'";

                                                                m_outWatcher = new ManagementEventWatcher(scope, q);

 

                                                                m_outWatcher.EventArrived += new EventArrivedEventHandler(OnUSBPlugout);

                                                                m_outWatcher.Start();

                                                }

                                                catch { }

                                }

 

                                public void OnUSBPlugout(object sender, EventArrivedEventArgs e)

                                {

                                                try {

                                                                string id = ((string[]) CDeviceControl.DeviceControl.Devices.GetCollection("SETTING")["USB"])[1];

                                                                foreach (PropertyData pd in e.NewEvent.Properties) {

                                                                                ManagementBaseObject mbo = null;

                                                                                if ((mbo = pd.Value as ManagementBaseObject) != null) {

                                                                                                foreach (PropertyData prop in mbo.Properties) {

                                                                                                                if (prop.Value != null && ((string) prop.Value).Contains(id)) {

                                                                                                                                CUSB usb= (CUSB) m_DataFlow.Connection;

                                                                                                                                CDeviceContext.DeviceContext.MainForm.BeginInvoke((MethodInvoker) delegate()

                                                                                                                                {

                                                                                                                                                m_outWatcher.Stop();

                                                                                                                                                m_DataFlow.OnClose -= OnDataFlowClose;

                                                                                                                                                CDeviceContext.DeviceContext.CloseAllChildFormsInMainForm();

                                                                                                                                                CDataSet.DataSet.DataFlow.Close();

                                                                                                                                                CDeviceContext.DeviceContext.SwitchContextToMain();

                                                                                                                                                CDataSet.DataSet.DataFlow = null;

                                                                                                                                                UsbMonitor = null;

                                                                                                                                                Utility.CMessages.ShowInformationMessage(

                                                                                                                                                                "USB device was unplugged. Please check the connection. /n",

                                                                                                                                                                "USB");

                                                                                                                                });

                                                                                                                }

                                                                                                }

                                                                                }

                                                                }

                                                }

                                                catch { }

                                }

 

                                public void MonitorPlugin()

                                {

                                                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'";

                                                                m_inWatcher = new ManagementEventWatcher(scope, q);

                                                                m_inWatcher.EventArrived += new EventArrivedEventHandler(OnUSBPlugin);

                                                                m_inWatcher.Start();

                                                }

                                                catch { }

                                }

 

                                public void OnUSBPlugin(object sender, EventArrivedEventArgs e)

                                {

                                                try {

                                                                string id = ((string[]) CDeviceControl.DeviceControl.Devices.GetCollection("SETTING")["USB"])[1];

                                                                foreach (PropertyData pd in e.NewEvent.Properties) {

                                                                                ManagementBaseObject mbo = null;

                                                                                if ((mbo = pd.Value as ManagementBaseObject) != null) {

                                                                                                foreach (PropertyData prop in mbo.Properties) {

                                                                                                                if (prop.Value != null && ((string) prop.Value).Contains(id)) {

                                                                                                                                CDeviceContext.DeviceContext.MainForm.BeginInvoke((MethodInvoker) delegate()

                                                                                                                                {

                                                                                                                                                Connect();

                                                                                                                                });

                                                                                                                }

                                                                                                }

                                                                                }

                                                                              }

                                                }

                                                catch { }

                                }

 

原创粉丝点击