C# WPF监听USB插入拨出

可以全部监听。好用

cs 复制代码
 private void FormF100WriteCortexLicense_Load(object sender, EventArgs e)
        {
            this.Text =  this.Text + " " + FT_Tools.Program.version;

            USB USBWatcher = new USB();
            USBWatcher.AddUSBEventWatcher(USBEventHandler, USBEventHandler, new TimeSpan(0, 0, 1));
        }
        private void USBEventHandler(Object sender, EventArrivedEventArgs e)
        {
            //暂未实现
            var watcher = sender as ManagementEventWatcher;
            watcher.Stop();
            //刷新设备信息
            if (e.NewEvent.ClassPath.ClassName == "__InstanceCreationEvent")
            {
                Log("Device Arrived");

            }
            else if (e.NewEvent.ClassPath.ClassName == "__InstanceDeletionEvent")
            {
                Log("Device Removed");
            }
            // 业务代码,逻辑耗时尽量不要太长,以免影响事件的监听
            watcher.Start();
        }

以下可以部分监听

cs 复制代码
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;

 

        protected override void WndProc(ref Message m)
        {

            try
            {
                if (m.Msg == WM_DEVICECHANGE)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case WM_DEVICECHANGE:
                            break;
                        case DBT_DEVICEARRIVAL:
                            MessageBox.Show("判断检测USB插入电脑");
                            DriveInfo[] s = DriveInfo.GetDrives();
                            foreach (DriveInfo drive in s)
                            {
                                if (drive.DriveType == DriveType.Removable)
                                {
                                    break;
                                }
                            }
                            break;
                        case DBT_CONFIGCHANGECANCELED:
                            break;
                        case DBT_CONFIGCHANGED:
                            break;
                        case DBT_CUSTOMEVENT:
                            break;
                        case DBT_DEVICEQUERYREMOVE:
                            break;
                        case DBT_DEVICEQUERYREMOVEFAILED:
                            break;
                        case DBT_DEVICEREMOVECOMPLETE:
                            MessageBox.Show("判断检测USB拔出电脑");
                            break;
                        case DBT_DEVICEREMOVEPENDING:
                            break;
                        case DBT_DEVICETYPESPECIFIC:
                            break;
                        case DBT_DEVNODES_CHANGED:
                            break;
                        case DBT_QUERYCHANGECONFIG:
                            break;
                        case DBT_USERDEFINED:
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            base.WndProc(ref m);
        }

以下不行

     public class USBWatcher
        {
            private MyAPI.MyDel Log;
            private ManagementEventWatcher watcher;
            public USBWatcher(MyAPI.MyDel Log)
            {
                this.Log= Log;
            }

            public void StartWatcher()
            {
                // 查询USB插入事件
                WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");

                // 创建事件监听器
                watcher = new ManagementEventWatcher(query);
                watcher.EventArrived += OnUSBInserted;

                // 开始监听
                watcher.Start();
            }

            public void StopWatcher()
            {
                // 停止监听
                if (watcher != null)
                {
                    watcher.Stop();
                    watcher.Dispose();
                }
            }

            private void OnUSBInserted(object sender, EventArrivedEventArgs e)
            {
                // 处理USB插入事件
                Log?.Invoke("USBInserted");
            }
        }
相关推荐
装不满的克莱因瓶几秒前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
biomooc8 分钟前
R 语言 | 绘图的文字格式(绘制上标、下标、斜体、文字标注等)
开发语言·r语言
骇客野人11 分钟前
【JAVA】JAVA接口公共返回体ResponseData封装
java·开发语言
black^sugar13 分钟前
纯前端实现更新检测
开发语言·前端·javascript
404NooFound18 分钟前
Python轻量级NoSQL数据库TinyDB
开发语言·python·nosql
用余生去守护1 小时前
python报错系列(16)--pyinstaller ????????
开发语言·python
数据小爬虫@1 小时前
利用Python爬虫快速获取商品历史价格信息
开发语言·爬虫·python
向宇it1 小时前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
莫名其妙小饼干2 小时前
网上球鞋竞拍系统|Java|SSM|VUE| 前后端分离
java·开发语言·maven·mssql
梦想平凡2 小时前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle