C#.NET U盘插拔监控
来源:互联网 发布:淘宝的万菊芬壶能买吗 编辑:程序博客网 时间:2024/06/17 12:43
【1】涉及的知识点
1) windows消息处理函数
protected override void WndProc(ref Message m)
捕获Message的系统硬件改变发出的系统消息
DriveInfo
【2】核心函数
消息常量:
/// <summary>/// windows消息常量/// </summary>class CWndProMsgConst{ public const int WM_DEVICECHANGE = 0x219; // 系统硬件改变发出的系统消息 public const int DBT_DEVICEARRIVAL = 0x8000;// 设备检测结束,并且可以使用 public const int DBT_CONFIGCHANGECANCELED = 0x0019; public const int DBT_CONFIGCHANGED = 0x0018; public const int DBT_CUSTOMEVENT = 0x8006; public const int DBT_DEVICEQUERYREMOVE = 0x8001; public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002; public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;// 设备卸载或者拔出 public const int DBT_DEVICEREMOVEPENDING = 0x8003; public const int DBT_DEVICETYPEHANGED = 0x0007; public const int DBT_QUERYCHANGSPECIFIC = 0x8005; public const int DBT_DEVNODES_CECONFIG = 0x0017; public const int DBT_USERDEFINED = 0xFFFF;}
扫描函数:
/// <summary> /// 扫描U口设备 /// </summary> private void ScanUSBDisk() { _usbdiskList.Clear(); DriveInfo[] drives = DriveInfo.GetDrives(); foreach (DriveInfo drive in drives) { if ((drive.DriveType == DriveType.Removable) && !drive.Name.Substring(0, 1).Equals("A")) { try { _usbdiskList.Add(drive.Name); } catch { MessageBox.Show("当前盘不能正确识别,请重新尝试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }
消息处理函数:
public void FillData(Form form, Message m, ListBox listbox) { _listbox = listbox; _form = form; try { if (m.Msg == CWndProMsgConst.WM_DEVICECHANGE) // 系统硬件改变发出的系统消息 { switch (m.WParam.ToInt32()) { case CWndProMsgConst.WM_DEVICECHANGE: break; //设备检测结束,并且可以使用 case CWndProMsgConst.DBT_DEVICEARRIVAL: { ScanUSBDisk(); _listbox.Items.Clear(); foreach (string str in _usbdiskList) { _listbox.Items.Add(str); } } break; // 设备卸载或者拔出 case CWndProMsgConst.DBT_DEVICEREMOVECOMPLETE: { ScanUSBDisk(); _listbox.Items.Clear(); foreach (string str in _usbdiskList) { _listbox.Items.Add(str); } } break; default: break; } } } catch (Exception ex) { MessageBox.Show("当前盘不能正确识别,请重新尝试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
完整的CS封装文件:
using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.Threading;using System.IO;namespace USBMonitor{ /// <summary> /// USB插拔监控类 /// </summary> public class CUSBMonitor { private delegate void SetTextCallback(string s); private IList<string> _usbdiskList = new List<string>(); private ListBox _listbox = null; private Form _form = null; public CUSBMonitor() { System.Timers.Timer timer = new System.Timers.Timer(1000); timer.Enabled = true; // 达到间隔时发生 timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerList); timer.AutoReset = false; // 仅在间隔第一次结束后引发一次 } public void FillData(Form form, Message m, ListBox listbox) { _listbox = listbox; _form = form; try { if (m.Msg == CWndProMsgConst.WM_DEVICECHANGE) // 系统硬件改变发出的系统消息 { switch (m.WParam.ToInt32()) { case CWndProMsgConst.WM_DEVICECHANGE: break; //设备检测结束,并且可以使用 case CWndProMsgConst.DBT_DEVICEARRIVAL: { ScanUSBDisk(); _listbox.Items.Clear(); foreach (string str in _usbdiskList) { _listbox.Items.Add(str); } } break; // 设备卸载或者拔出 case CWndProMsgConst.DBT_DEVICEREMOVECOMPLETE: { ScanUSBDisk(); _listbox.Items.Clear(); foreach (string str in _usbdiskList) { _listbox.Items.Add(str); } } break; default: break; } } } catch (Exception ex) { MessageBox.Show("当前盘不能正确识别,请重新尝试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } /// <summary> /// 设置USB列表 /// </summary> void TimerList(object sender, System.Timers.ElapsedEventArgs e) { ScanUSBDisk(); foreach (string str in _usbdiskList) { SetText(str); } } /// <summary> /// 扫描U口设备 /// </summary> private void ScanUSBDisk() { _usbdiskList.Clear(); DriveInfo[] drives = DriveInfo.GetDrives(); foreach (DriveInfo drive in drives) { if ((drive.DriveType == DriveType.Removable) && !drive.Name.Substring(0, 1).Equals("A")) { try { _usbdiskList.Add(drive.Name); } catch { MessageBox.Show("当前盘不能正确识别,请重新尝试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } /// <summary> /// 设置List列表 /// </summary> /// <param name="text">名称</param> public void SetText(string text) { if (_listbox == null) return; if (this._listbox.InvokeRequired) // 调用方位于创建控件所在的线程以外的线程中 { if (_listbox.Items.Contains(text)) return; SetTextCallback d = new SetTextCallback(SetText); _form.Invoke(d, new object[] { text }); } else { if (_listbox.Items.Contains(text)) return; this._listbox.Items.Add(text); } } } /// <summary> /// windows消息常量 /// </summary> class CWndProMsgConst { public const int WM_DEVICECHANGE = 0x219; // 系统硬件改变发出的系统消息 public const int DBT_DEVICEARRIVAL = 0x8000;// 设备检测结束,并且可以使用 public const int DBT_CONFIGCHANGECANCELED = 0x0019; public const int DBT_CONFIGCHANGED = 0x0018; public const int DBT_CUSTOMEVENT = 0x8006; public const int DBT_DEVICEQUERYREMOVE = 0x8001; public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002; public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;// 设备卸载或者拔出 public const int DBT_DEVICEREMOVEPENDING = 0x8003; public const int DBT_DEVICETYPEHANGED = 0x0007; public const int DBT_QUERYCHANGSPECIFIC = 0x8005; public const int DBT_DEVNODES_CECONFIG = 0x0017; public const int DBT_USERDEFINED = 0xFFFF; } }
测试窗体(重写消息函数):
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace USBMonitor{ public partial class Main : Form { public Main() { InitializeComponent(); } CUSBMonitor usbMonitor = new CUSBMonitor(); protected override void WndProc(ref Message m) { usbMonitor.FillData(this, m, _listBox); base.WndProc(ref m); } }}
0 0
- C#.NET U盘插拔监控
- linux c监控U盘插拔事件
- U 盘文件监控
- linux中的hotplug技术实现监控u盘插拔
- QSocketNotifier 实现文件监控(demo Linux系统U盘插拔监控)
- C#.Net 鼠标和键盘全局监控勾子(hook )
- C#.NET MSCHART 实现数据监控并自动滚动
- QT实现U盘监控
- U-net神经网络
- FCN和U-Net
- U-net 图像分割
- FCN和U-Net
- U/V-Net Brief
- U-net翻译
- FCN 和 U-net
- FCN&U-Net
- net 监控方法执行时间
- C# 监控U盘插入与拔出
- C语言中,头文件和源文件的关系(转)
- Oracle dataguard报错:Error 1017 received logging on to the standby
- DBInputformat 理解
- 逻辑推理与判断(哪个大夫哪天值班)
- poj 1006 Biorhythms 中国剩余定理
- C#.NET U盘插拔监控
- LeetCode --- 1. Two Sum
- 生物化学“糖代谢”教学中的PBL教学模式
- 在GDB里plot数据
- C#控制台设置
- hive演示实验--sogou日志数据
- 逻辑推理与判断(区分旅客国籍)
- NDK开发学习- findLibrary returned null错误
- Html5中JSON对象与String的互相转换