惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - 云梦鸿

Winform界面显示的“语言”切换 Linux使用笔记 QT 练习笔记 QT 读写配置文件(*.INI) ubuntu 配置网口静态IP C++ 读写文件 C#编写Windows服务 C#代码计算农历(日期、节气、节日) 中标麒麟,使用笔记 关于无密码访问 Windows7/10 的远程桌面/共享 颜色转换:HSB颜色 与 RGB颜色 QT 信号(槽)绑定的使用_connect QT 给控件(Label)设置显示图片 QT 打包Windows应用程序(*.exe) C#程序执行Python脚本 C# AnimateWindow 设置窗体动画 C# GetWindowRect 获取窗体在屏幕中的位置信息 C# 创建音频WAVE文件头信息(*.wav) VS2019错误:CS8370 的处理方法
C#监控U盘插拔
云梦鸿 · 2021-01-20 · via 博客园 - 云梦鸿

关键实现1:

    扫描所有存储设备,筛选出U盘

        private void ScanDisk()
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (var drive in drives)
            {
                // 可移动存储设备,且不是A盘
                if ((drive.DriveType == DriveType.Removable) && false == drive.Name.Substring(0, 1).Equals("A"))
                {
                    Console.WriteLine("找到一个U盘:" + drive.Name);
                }
            }
        }

关键实现2:

    监听系统消息,在加载U盘时处理

        const int WM_DeviceChange = 0x219;   // 系统硬件改变发出的系统消息
        const int DBT_DeviceArrival = 0x8000;   // 设备检测结束,并可以使用
        const int DBT_DeviceRemoveComplete = 0x8004;// 设备移除

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == WM_DeviceChange) // 系统硬件改变发出的系统消息
            {
                switch (m.WParam.ToInt32())
                {
                    case WM_DeviceChange:
                        break;
                    case DBT_DeviceArrival:
                        ScanDisk(); // 扫描所有满足特征的设备
                        break;
                    case DBT_DeviceRemoveComplete:
                        ScanDisk();
                        break;
                    default:
                        break;
                }
            }
        }

扩展:相关的消息列表

        const int DBT_ConfigChangeCanceled = 0x0019;
        const int DBT_ConfigChanged = 0x0018;
        const int DBT_CustomEvent = 0x8006;
        const int DBT_DeviceQueryRemove = 0x8001;
        const int DBT_DeviceQueryRemoveFailed = 0x8002;
        const int DBT_DeviceRemovePending = 0x8003;
        const int DBT_DeviceTypeHanged = 0x8007;
        const int DBT_QueryChangSpecific = 0x8005;
        const int DBT_DevNodes_CEConfig = 0x0017;
        const int DBT_UserDefined = 0xFFFF;