【问题标题】:How can i detect my digital camera connected to my pc usb ? Also the camera name如何检测连接到我的电脑 USB 的数码相机?还有相机名称
【发布时间】:2015-12-17 14:41:36
【问题描述】:

在form1的顶部:

class USBDeviceInfo
        {
            public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
            {
                this.DeviceID = deviceID;
                this.PnpDeviceID = pnpDeviceID;
                this.Description = description;
            }
            public string DeviceID { get; private set; }
            public string PnpDeviceID { get; private set; }
            public string Description { get; private set; }
        }

List<string> cameras = new List<string>();

在构造函数中:

var usbDevices = GetUSBDevices();
                foreach (var usbDevice in usbDevices)
                {
                    cameras.Add(usbDevice.DeviceID);
                    cameras.Add(usbDevice.PnpDeviceID);
                    cameras.Add(usbDevice.Description);
                }

GetUSBDevices() 方法:

static List<USBDeviceInfo> GetUSBDevices()
        {
            List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

            ManagementObjectCollection collection;
            using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
                collection = searcher.Get();

            foreach (var device in collection)
            {
                devices.Add(new USBDeviceInfo(
                (string)device.GetPropertyValue("DeviceID"),
                (string)device.GetPropertyValue("PNPDeviceID"),
                (string)device.GetPropertyValue("Description")
                ));
            }

            collection.Dispose();
            return devices;
        }

我在相机列表中得到的是 3 项:

在索引 0 中:USB\ROOT_HUB20\4&5E0D3C3&0

在索引 1:USB\ROOT_HUB20\4&5E0D3C3&0

indes 2:USB 根集线器

但我要做的是首先检测我的数码相机连接到 USB。 然后得到相机名称这个字母和数字,如 4&5E0D3C3&0 对我来说并不多。 最后能够从相机 sd 卡中复制删除文件,就像我正在做的目录一样,例如在 c:\

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    您应该使用Win32_USBControllerDevice 而不是Win32_USBHub,然后使用Win32_PNPEntity 来获取有关设备本身的信息。看到这个链接 例如:

    http://blogs.technet.com/b/heyscriptingguy/archive/2005/03/15/how-can-i-determine-which-usb-devices-are-connected-to-a-computer.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 2013-11-10
      • 1970-01-01
      相关资源
      最近更新 更多