C#: 监测 USB plugin and plugout

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 { }

                                }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值