最近项目需要需要试试采集电脑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:\n", 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 如果谁有文档还望 也给我一份


转载于:https://www.cnblogs.com/leoxjy/p/9777494.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐