• 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. }

  • 相关阅读:
    如何使tstringgrid中心的单元格文本对齐
    ubuntu18.04离线源制作
    Win11 删除文件时提示“找不到该项目,请重试”的解决办法
    三思近10000㎡天幕屏耀显上海“八万人”体育场
    C++深度优先搜索(DFS)算法的应用:树中可以形成回文的路径数
    当公路的规则与秩序,被昇腾AI时刻守护
    如何用matlab搭建激光器
    基于可逆网络的单一图像超分辨率
    SQL Server - 提高服务器安全性13招
    Mac M1 jdk11 DNS错误
  • 原文地址:https://blog.csdn.net/chenhao0568/article/details/132548089