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");
            }
        }
相关推荐
c4fx7 分钟前
Delphi5利用DLL实现窗体的重用
开发语言·delphi·dll
阿华的代码王国12 分钟前
MySQL ------- 索引(B树B+树)
数据库·mysql
鸽芷咕30 分钟前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
Hello.Reader40 分钟前
StarRocks实时分析数据库的基础与应用
大数据·数据库
Jhxbdks40 分钟前
C语言中的一些小知识(二)
c语言·开发语言·笔记
java66666888840 分钟前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存41 分钟前
源码分析:LinkedList
java·开发语言
执键行天涯42 分钟前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
代码雕刻家43 分钟前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构
Fan_web44 分钟前
JavaScript高级——闭包应用-自定义js模块
开发语言·前端·javascript·css·html