zoukankan      html  css  js  c++  java
  • WPF USB设备采集开源工具介绍

    最近项目需要需要试试采集电脑USB 设备信息 找到了 开源 示例 代码非常好  公共库也很友好  可能知名度太低   才4star  

    https://github.com/vurdalakov/usbdevices.git

    示例图

     代码结构

     UsbDevicesDotNet是公共库

    UsbDevicesViewer是WPF界面

    别的是控制台测试

    获取电脑所有USB设备信息 

    List<UsbDevice> UsbDevices = UsbDevice.GetDevices(new Guid(UsbDeviceWinApi.GUID_DEVINTERFACE_USB_DEVICE)).ToList()
    

     

     设备事件

            private Win32UsbControllerDevices win32UsbControllerDevices = new Win32UsbControllerDevices();
            private DeviceManagementNotifications deviceManagementNotifications = new DeviceManagementNotifications();
    
    
    
            private void EnableDeviceWatcher(Boolean enable)
            {
                if (enable)
                {
                    this.win32UsbControllerDevices.DeviceConnected += OnWin32UsbControllerDevicesDeviceConnected;
                    this.win32UsbControllerDevices.DeviceDisconnected += OnWin32UsbControllerDevicesDeviceDisconnected;
                    //this.win32UsbControllerDevices.DeviceModified += OnWin32UsbControllerDevicesDeviceModified;
    
                    this.win32UsbControllerDevices.StartWatcher();
    
                    this.deviceManagementNotifications.DeviceConnected += OnDeviceManagementNotificationsDeviceConnected;
                    this.deviceManagementNotifications.DeviceDisconnected += OnDeviceManagementNotificationsDeviceDisconnected;
    
                    this.deviceManagementNotifications.Start(new Guid(UsbDeviceWinApi.GUID_DEVINTERFACE_USB_DEVICE));
                }
                else
                {
                    this.deviceManagementNotifications.Stop();
    
                    //this.deviceManagementNotifications.DeviceConnected -= OnDeviceManagementNotificationsDeviceConnected;
                    //this.deviceManagementNotifications.DeviceDisconnected -= OnDeviceManagementNotificationsDeviceDisconnected;
    
                    this.win32UsbControllerDevices.StopWatcher();
    
                    this.win32UsbControllerDevices.DeviceConnected -= OnWin32UsbControllerDevicesDeviceConnected;
                    this.win32UsbControllerDevices.DeviceDisconnected -= OnWin32UsbControllerDevicesDeviceDisconnected;
                    //this.win32UsbControllerDevices.DeviceModified -= OnWin32UsbControllerDevicesDeviceModified;
                }
            }
    
    /// <summary>
            /// 设备插入事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void OnWin32UsbControllerDevicesDeviceConnected(object sender, Win32UsbControllerDeviceEventArgs e)
            {
                var _thisDevice = e.Device;
            }
    
            /// <summary>
            /// 设备拔出事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void OnWin32UsbControllerDevicesDeviceDisconnected(Object sender, Win32UsbControllerDeviceEventArgs e)
            {
                var _thisDevice = e.Device;
            }
    
    
            /// <summary>
            /// 设备管理器广播插入
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void OnDeviceManagementNotificationsDeviceConnected(Object sender, DeviceManagementNotificationsEventArgs e)
            {
                var _DeviceClass = e.DeviceClass;
                var _DevicePath = e.DevicePath;
            }
            /// <summary>
            /// 设备管理器广播拔出
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void OnDeviceManagementNotificationsDeviceDisconnected(Object sender, DeviceManagementNotificationsEventArgs e)
            {
                var _DeviceClass = e.DeviceClass;
                var _DevicePath = e.DevicePath;
            }
    

     

    控制台示例

    
    
    static void Main(string[] args)
            {
                UsbDevice[] usbDevices = UsbDevice.GetDevices();
    
                Console.WriteLine("{0} USB devices:
    ", usbDevices.Length);
    
                foreach (UsbDevice usbDevice in usbDevices)
                {
                    Console.WriteLine("VID_{0}&PID_{1}", usbDevice.Vid, usbDevice.Pid);
                    Console.WriteLine("Hub:Port = {0}:{1}", usbDevice.Hub, usbDevice.Port);
                    Console.WriteLine("Device ID     = {0}", usbDevice.DeviceId);
    
                    foreach (var deviceInterface in usbDevice.Interfaces)
                    {
                        Console.WriteLine("Interface ID  = {0}", deviceInterface.InterfaceId);
                    }
    
                    Console.WriteLine("LocationInformation = {0}", usbDevice.GetRegistryPropertyValue(UsbDeviceWinApi.DeviceRegistryPropertyKeys.SPDRP_LOCATION_INFORMATION));
    
                    foreach (String locationPath in usbDevice.GetRegistryPropertyValue(UsbDeviceWinApi.DeviceRegistryPropertyKeys.SPDRP_LOCATION_PATHS) as String[])
                    {
                        Console.WriteLine("LocationPaths = {0}", locationPath);
                    }
    
                    if (!String.IsNullOrEmpty(usbDevice.BusReportedDeviceDescription))
                    {
                        Console.WriteLine("DeviceDescription = {0}", usbDevice.BusReportedDeviceDescription);
                    }
    
                    Console.WriteLine();
                    
                }
                Console.ReadLine();
            }
    

      


    没有找到该示例的文档 全靠看demo 如果谁有文档还望 也给我一份


  • 相关阅读:
    POJ1785 Binary Search Heap Construction
    Bzoj1185 [HNOI2007]最小矩形覆盖
    POJ2409 Let it Bead
    Bzoj2732 [HNOI2012]射箭
    Bzoj4515 [Sdoi2016]游戏
    Bzoj3925 [Zjoi2015]地震后的幻想乡
    Bzoj3223 Tyvj 1729 文艺平衡树
    COGS2642 / Bzoj4590 [Shoi2015]自动刷题机
    Bzoj1313 [HAOI2008]下落的圆盘
    python——描述符
  • 原文地址:https://www.cnblogs.com/leoxjy/p/9777494.html
Copyright © 2011-2022 走看看