如何获得连接到我的系统在.net中所有USB设备的VID和PID? [英] How to get the VID and PID of all USB devices connected to my system in .net?

查看:857
本文介绍了如何获得连接到我的系统在.net中所有USB设备的VID和PID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个C#.NET控制台应用程序,什么code我可以写在屏幕上显示连接到系统的每个USB设备的VID和PID?

In a C#.net console application, what code can i write to print on the screen the VID and PID of each USB device connected to the system?

推荐答案

下面是一些code,我根据我发现到目前为止写的。有可能是一个更好的办法来做到这一点。

Here's some code I've written based on what I've found so far. There may be a better way to do this.

    public static void MyMethod()
    {                            
        System.Management.ManagementClass USBClass = new ManagementClass("Win32_USBDevice");                
        System.Management.ManagementObjectCollection USBCollection = USBClass.GetInstances();

        foreach (System.Management.ManagementObject usb in USBCollection)
        {
            string deviceId = usb["deviceid"].ToString();
            Console.WriteLine(deviceId);

            int vidIndex = deviceId.IndexOf("VID_");
            string startingAtVid = deviceId.Substring(vidIndex + 4); // + 4 to remove "VID_"                    
            string vid = startingAtVid.Substring(0, 4); // vid is four characters long
            Console.WriteLine("VID: " + vid);

            int pidIndex = deviceId.IndexOf("PID_");
            string startingAtPid = deviceId.Substring(pidIndex + 4); // + 4 to remove "PID_"                    
            string pid = startingAtPid.Substring(0, 4); // pid is four characters long
            Console.WriteLine("PID: " + pid);                                        
        }            

        Console.ReadLine();
    }

这篇关于如何获得连接到我的系统在.net中所有USB设备的VID和PID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆