C#监控USB接口

这篇博客介绍了如何使用C#编程实现对USB接口的监控,能够检测到USB设备的插入和移除,特别是针对多个U盘的操作。
摘要由CSDN通过智能技术生成

该C#代码实现监控USB接口是否有设备接入或拨出,包括多个U盘。

  1 using System;
  2 using System.IO;
  3 using System.Runtime.InteropServices;
  4 using System.Windows.Forms;
  5 
  6 namespace MyApplication
  7 {
  8     public partial class Form1 : Form
  9     {
 10         const int WM_DEVICECHANGE = 0x0219; // 发生设备变动
 11         const int DBT_DEVICEARRIVAL = 0x8000; // 系统检测到一个新设备
 12         const int DBT_DEVTYP_VOLUME = 0x00000002; // 逻辑卷标
 13         const int DBT_CONFIGCHANGECANCELED = 0x0019;
 14         const int DBT_CONFIGCHANGED = 0x0018;
 15         const int DBT_CUSTOMEVENT = 0x8006;
 16         const int DBT_DEVICEQUERYREMOVE = 0x8001;
 17         const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002; // 系统完成移除一个设备
 18         const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
 19         const int DBT_DEVICEREMOVEPENDING = 0x8003;
 20         const int DBT_DEVICETYPESPECIFIC = 0x8005;
 21         const int DBT_DEVNODES_CHANGED = 0x0007;
 22         const int DBT_QUERYCHANGECONFIG = 0x0017;
 23         const int DBT_USERDEFINED = 0xFFFF;
 24 
 25         [StructLayout(LayoutKind.Sequential)]
 26         public struct DEV_BROADCAST_VOLUME
 27         {
 28             public int dbcv_size;
 29             public int dbcv_devicetype;
 30             public int dbcv_reserved;
 31             public int dbcv_unitmask;
 32         }
 33 
 34         public Form1()
 35         {
 36             InitializeComponent();
 37         }
 38 
 39         protected override void WndProc(ref Message m)
 40         {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值