C#学习——识别U盘
来源:互联网 发布:查看淘宝客佣金 编辑:程序博客网 时间:2024/06/05 11:57
首先,定义USB相关驱动的消息常量,如下,其中WM_DEVICECHANGE表示有设备发生变化,对USB插入和拔出事件来说,只有只需要定义两个:DBT_DEVICEARRIVAL(插入设备)和DBT_DEVICEREMOVECOMPLETE(拔出设备)。所有的消息常量如下:
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_DEVICETYPESPECIFIC = 0x8005; public const int DBT_DEVNODES_CHANGED = 0x0007; public const int DBT_QUERYCHANGECONFIG = 0x0017; public const int DBT_USERDEFINED = 0xFFFF;定义好消息常量之后,需要重写WndProc,如下:
protected override void WndProc(ref Message m){ try { if (m.Msg == WM_DEVICECHANGE) { switch (m.WParam.ToInt32()) { case DBT_DEVICEARRIVAL: //USB插入时要处理的事件 break; case DBT_DEVICEREMOVECOMPLETE: //USB拔出时要处理的事件 break; default: break; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } base.WndProc(ref m);}重写代码时只列出了USB插入和拔出时处理方法,如果想添加其他的可以依照这两个添加。
如果想获取插入可移动设备的盘符,可以用以下代码:
foreach (DriveInfo drive in DriveInfo.GetDrives()) { if (drive.DriveType == DriveType.Removable) { Text = "盘符:" + drive.Name.ToString(); break; } }
阅读全文
0 0
- C#学习——识别U盘
- C#识别U盘和盘符
- C#识别连接到PC的U盘
- C#写个程序能识别有U盘插入
- linux磁盘管理——识别和使用u盘
- U盘识别操作
- U盘不能被识别
- u盘无法识别怎么办
- linux下识别U盘
- 请教:U盘不识别
- ubuntu9 如何识别u盘
- 系统无法识别U盘
- Win7无法识别U盘
- Linux 如何识别U盘
- fedora 无法识别u盘
- linux识别ntfs U盘
- U盘识别不了怎么办
- Linux如何识别U盘
- 扔盘子
- 20170927_快排应用_数组中寻找第K大的数字
- 顺序表应用3:元素位置互换之移位算法
- Find the maximum of the three numbers(Take this opportunity to review the sorting algorithm)
- python 并发编程
- C#学习——识别U盘
- 十分钟搞定pandas
- 牛客网编程题:进制均值
- dynamics.js
- 学习笔记08
- Mybatis利用SQL insert标签时报错:无效的列类型
- mac下安装mysql
- html中块级标签,行内标签,空元素的总结
- IOS控件列---仿Android的Toast提示信息