简体   繁体   English

USB设备已连接

[英]USB Device Connected

I'm trying to make a function that detects if a usb device is connected given the devices pid and vid. 我正在尝试创建一个函数,在设备pid和vid的情况下检测usb设备是否已连接。 I'm hoping it would look something like this, I'm just not sure how to do this in C#. 我希望它看起来像这样,我只是不确定如何在C#中做到这一点。

public bool IsUsbDeviceConnected(string pid, string vid)
{
  //Code here
}
//using System.Management
public bool IsUsbDeviceConnected(string pid, string vid)
{   
  using (var searcher = 
    new ManagementObjectSearcher(@"Select * From Win32_USBControllerDevice"))
  {
    using (var collection = searcher.Get())
    {
      foreach (var device in collection)
      {
        var usbDevice = Convert.ToString(device);

        if (usbDevice.Contains(pid) && usbDevice.Contains(vid))
          return true;
      }
    }
  }
  return false;
}

may be something like 可能是这样的

//import the System.Management namespace at the top in your "using" statement. Then in a method, or on a button click:

ManagementObjectCollection collection;
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'"))
  collection = searcher.Get();
foreach (ManagementObject currentObject in collection)
{
  ManagementObject theSerialNumberObjectQuery = new ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'");
  MessageBox.Show(theSerialNumberObjectQuery["SerialNumber"].ToString());
}
collection.Dispose();

Using WMI 使用WMI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在.NET中获取连接的USB设备的型号名称 - Getting the model name of a connected USB device in .NET 如何与USB连接的Win CE设备进行通信 - How to communicate with Win CE device connected by usb 获取已连接USB设备的端口名称 - Getting the port name of a connected USB device 与设备连接时确定是否启用USB调试 - Determine usb debugging enabled or not when connected with device C#如何从多个连接相同的USB设备中找出特定的USB设备? - C# How to find out particular USB device from multiple connected same USB device? 从USB连接的android设备连接到笔记本电脑localhost - Connecting to my laptops localhost from my USB connected android device 如何在Windows Forms应用程序中检测连接到USB的移动设备(Iphone)? - How to detect mobile device(Iphone) connected to USB in windows forms application? 如何获取有关最近连接的 USB 设备的信息? - How do I get information about recently connected USB device? 查找与PC连接的USB /串行通信设备需要什么信息 - What information is required to find connected USB/Serial communication device with PC Web应用程序,需要确定是否将USB设备连接到客户端计算机 - web application, need to determine if a USB device is connected to the client computer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM