• C# WPF监听USB插入拨出


    可以全部监听。好用

     

    1. private void FormF100WriteCortexLicense_Load(object sender, EventArgs e)
    2. {
    3. this.Text = this.Text + " " + FT_Tools.Program.version;
    4. USB USBWatcher = new USB();
    5. USBWatcher.AddUSBEventWatcher(USBEventHandler, USBEventHandler, new TimeSpan(0, 0, 1));
    6. }
    7. private void USBEventHandler(Object sender, EventArrivedEventArgs e)
    8. {
    9. //暂未实现
    10. var watcher = sender as ManagementEventWatcher;
    11. watcher.Stop();
    12. //刷新设备信息
    13. if (e.NewEvent.ClassPath.ClassName == "__InstanceCreationEvent")
    14. {
    15. Log("Device Arrived");
    16. }
    17. else if (e.NewEvent.ClassPath.ClassName == "__InstanceDeletionEvent")
    18. {
    19. Log("Device Removed");
    20. }
    21. // 业务代码,逻辑耗时尽量不要太长,以免影响事件的监听
    22. watcher.Start();
    23. }

    以下可以部分监听

    1. public const int WM_DEVICECHANGE = 0x219;
    2. public const int DBT_DEVICEARRIVAL = 0x8000;
    3. public const int DBT_CONFIGCHANGECANCELED = 0x0019;
    4. public const int DBT_CONFIGCHANGED = 0x0018;
    5. public const int DBT_CUSTOMEVENT = 0x8006;
    6. public const int DBT_DEVICEQUERYREMOVE = 0x8001;
    7. public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
    8. public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
    9. public const int DBT_DEVICEREMOVEPENDING = 0x8003;
    10. public const int DBT_DEVICETYPESPECIFIC = 0x8005;
    11. public const int DBT_DEVNODES_CHANGED = 0x0007;
    12. public const int DBT_QUERYCHANGECONFIG = 0x0017;
    13. public const int DBT_USERDEFINED = 0xFFFF;
    14. protected override void WndProc(ref Message m)
    15. {
    16. try
    17. {
    18. if (m.Msg == WM_DEVICECHANGE)
    19. {
    20. switch (m.WParam.ToInt32())
    21. {
    22. case WM_DEVICECHANGE:
    23. break;
    24. case DBT_DEVICEARRIVAL:
    25. MessageBox.Show("判断检测USB插入电脑");
    26. DriveInfo[] s = DriveInfo.GetDrives();
    27. foreach (DriveInfo drive in s)
    28. {
    29. if (drive.DriveType == DriveType.Removable)
    30. {
    31. break;
    32. }
    33. }
    34. break;
    35. case DBT_CONFIGCHANGECANCELED:
    36. break;
    37. case DBT_CONFIGCHANGED:
    38. break;
    39. case DBT_CUSTOMEVENT:
    40. break;
    41. case DBT_DEVICEQUERYREMOVE:
    42. break;
    43. case DBT_DEVICEQUERYREMOVEFAILED:
    44. break;
    45. case DBT_DEVICEREMOVECOMPLETE:
    46. MessageBox.Show("判断检测USB拔出电脑");
    47. break;
    48. case DBT_DEVICEREMOVEPENDING:
    49. break;
    50. case DBT_DEVICETYPESPECIFIC:
    51. break;
    52. case DBT_DEVNODES_CHANGED:
    53. break;
    54. case DBT_QUERYCHANGECONFIG:
    55. break;
    56. case DBT_USERDEFINED:
    57. break;
    58. default:
    59. break;
    60. }
    61. }
    62. }
    63. catch (Exception ex)
    64. {
    65. MessageBox.Show(ex.Message);
    66. }
    67. base.WndProc(ref m);
    68. }

    以下不行 

    1. public class USBWatcher
    2. {
    3. private MyAPI.MyDel Log;
    4. private ManagementEventWatcher watcher;
    5. public USBWatcher(MyAPI.MyDel Log)
    6. {
    7. this.Log= Log;
    8. }
    9. public void StartWatcher()
    10. {
    11. // 查询USB插入事件
    12. WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
    13. // 创建事件监听器
    14. watcher = new ManagementEventWatcher(query);
    15. watcher.EventArrived += OnUSBInserted;
    16. // 开始监听
    17. watcher.Start();
    18. }
    19. public void StopWatcher()
    20. {
    21. // 停止监听
    22. if (watcher != null)
    23. {
    24. watcher.Stop();
    25. watcher.Dispose();
    26. }
    27. }
    28. private void OnUSBInserted(object sender, EventArrivedEventArgs e)
    29. {
    30. // 处理USB插入事件
    31. Log?.Invoke("USBInserted");
    32. }
    33. }

  • 相关阅读:
    【Python】创建一个简易服务器并实现移动端快速访问电脑文件
    repr函数输出调试信息
    Google 为造芯再掀“抢人大战”,英特尔 17 年老将加入
    【10.28】【VP】Codeforces Round #743 (Div. 2)
    C/C++教程 从入门到精通《第二章》—— 快速入门C/C++
    什么是腾讯云主机安全,主要有哪些功能作用?
    react钩子函数用法(useState、useEffect、useContext)
    【JAVA】JSP
    Hadoop-HA-Hive-on-Spark 4台虚拟机安装配置文件
    【好文鉴赏】初创公司到底值不值得去?从以下几点考虑
  • 原文地址:https://blog.csdn.net/chenhao0568/article/details/132548089