C#: 监测 USB plugin and plugout
2008-07-16 21:29
435 查看
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 { }
}
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 { }
}
相关文章推荐
- msm8937 SDP plugin and plugout log
- Understanding ref And out With C# 7
- C# : ref out params and override
- DEMO[C#]refKeyword and outKeyword 关于ref与out关键字区别的DEMO
- 【原创】我的AVRUSB Receiver (C# And C)---2008.07
- Use C# and the .NET Framework to develop your own plugin architecture
- DEMO[C#]refKeyword and outKeyword 关于ref与out关键字区别的DEMO
- C# ref and out
- [置顶] 【原创】我的AVRUSB Receiver (C# And C)---2008.07
- Usage of keywords "in" and "out" in C#(.NET 4.0)
- C# USB Detection - winform and WPF
- Web Zoom in and Zoom out plugin
- Build a Desktop GIS Application Using MapWinGIS and C#
- KeyAndMouse - 鼠标及键盘动作监测组件
- Scale up and Scale out
- C# Parse and TryParse 方法详解
- C#、ASP.NET中ref和out的区别
- VirtualBox Guest Addtions and USB Extension Pack,Ghost and Host share USB device
- C#基础:ref和out的区别
- Codeforces_714E:Sonya_and_Problem_Wihtout_a_Legend(DP+想法题)