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");
            }
        }
相关推荐
荒川之神几秒前
ORACLE _11G_R2_ASM 常用命令
数据库·oracle
秋の花3 分钟前
【JAVA基础】Java集合基础
java·开发语言·windows
IT培训中心-竺老师6 分钟前
Oracle 23AI创建示例库
数据库·oracle
小松学前端6 分钟前
第六章 7.0 LinkList
java·开发语言·网络
可峰科技15 分钟前
斗破QT编程入门系列之二:认识Qt:编写一个HelloWorld程序(四星斗师)
开发语言·qt
全栈开发圈19 分钟前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫
面试鸭24 分钟前
离谱!买个人信息买到网安公司头上???
java·开发语言·职场和发展
小白学大数据24 分钟前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
Python大数据分析@27 分钟前
python操作CSV和excel,如何来做?
开发语言·python·excel