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");
            }
        }
相关推荐
工业甲酰苯胺5 分钟前
Redis性能优化的18招
数据库·redis·性能优化
没书读了1 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
----云烟----1 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024061 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
开心工作室_kaic2 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it2 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
i道i2 小时前
MySQL win安装 和 pymysql使用示例
数据库·mysql
小怪兽ysl2 小时前
【PostgreSQL使用pg_filedump工具解析数据文件以恢复数据】
数据库·postgresql
武子康2 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
九鼎科技-Leo2 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf