C#获取USB事件API的代码

727 阅读1分钟
将开发过程比较重要的一些内容做个珍藏,如下的内容段是关于C#获取USB事件API的内容。 

const int WM_DEVICECHANGE = 0x2190;
        const int DBT_DEVICEARRIVAL = 0x8000;
        const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
        protected override void WndProc(ref Message m)
        {
            try
            {
                    switch (m.WParam.ToInt32())
                    {
                            DriveInfo[] s = DriveInfo.GetDrives();
                            foreach (DriveInfo drive in s)
                            {
                                if (drive.DriveType == DriveType.Removable)
                                {
Console.WriteLine("USB插入");
break;
                                }
                            }
                            break;
                            Console.WriteLine("USB卸载");
                            break;
                        default:
                            break;
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            base.WndProc(ref m);
        }