如何在.net中获取连接到我的系统的所有USB设备的VID和PID?

发布于 2024-12-18 09:01:38 字数 68 浏览 2 评论 0原文

在 C#.net 控制台应用程序中,我可以编写什么代码来在屏幕上打印连接到系统的每个 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满身野味 2024-12-25 09:01:38

这是我根据迄今为止发现的内容编写的一些代码。可能有更好的方法来做到这一点。

    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();
    }

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文