获取USB驱动器的硬件ID. [英] Get Hardware Id of USB Drive.
本文介绍了获取USB驱动器的硬件ID.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在用C ++/MFC(不是C#)开发程序,我想知道有什么方法可以获取USB驱动器的设备ID/硬件ID.
谢谢我的进步.
Hi to all,
i am developing a program in C++/MFC (not in C#) and i want to know is there any method by which i can get device id / hardware id of USB drives.
thanks i advance.
推荐答案
通过下面的CP链接
http://www.codeproject.com/KB/mcpp/DriveInfoEx.aspx [ ^ ]
Go through the below CP link
http://www.codeproject.com/KB/mcpp/DriveInfoEx.aspx[^]
我该怎么办建议是通过winapi使用查询WMI来获取vid和pid
我不熟悉MFC,但winapi
what I suggest is to use query WMI through winapi to get vid and pid
I don''t familiar with MFC but winapi
命名空间ConsoleApplication1
{
使用System.Text;
使用System.Data.Odbc;
使用System.Data;
使用System.Web;
使用System.ComponentModel;
使用System.IO;
使用System.Net;
使用System.Diagnostics;
使用System.Runtime.InteropServices;
使用System.Data.OleDb;
使用System.Text.RegularExpressions;
使用System.Linq;
使用系统;
使用System.Collections.Generic;
使用System.Management; //需要将System.Management添加到您的项目引用中.
课程计划
{
静态void Main(string [] args)
{
var usbDevices = GetUSBDevices();
foreach(usbDevices中的var usbDevice)
{
字符串m_pendid;
Console.WriteLine(设备ID:{0},PNP设备ID:{1},描述:{2},USBVersion:{3},SystemName:{4}",
usbDevice.DeviceID,usbDevice.PnpDeviceID,usbDevice.Description,usbDevice.usbversion,usbDevice.SystemName);
//m_pendid = catch ["usbDevice.DeviceID"];
m_pendid = usbDevice.DeviceID;
Console.WriteLine("Test" + m_pendid);
}
//Console.Write("DeviceID:DeviceID");
Console.Read();
}
静态列表< usbdeviceinfo> GetUSBDevices()
{
列出< usbdeviceinfo> devices = new List< usbdeviceinfo>();
ManagementObjectCollection集合;
使用(var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
集合= searcher.Get();
foreach(集合中的var设备)
{
devices.Add(新的USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description"),
(string)device.GetPropertyValue("USBVersion"),
(string)device.GetPropertyValue("SystemName")
));
}
collection.Dispose();
返回设备;
}
}
类USBDeviceInfo
{
公共USBDeviceInfo(字符串deviceID,字符串pnpDeviceID,字符串描述,字符串usbversion1,字符串SystemName2)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description =说明;
this.usbversion = usbversion1;
this.SystemName = SystemName2;
}
公用字符串DeviceID {私人套装; }
公共字符串PnpDeviceID {get;私人套装; }
公共字符串说明{私人套装; }
公共字符串usbversion {get;私人套装; }
公共字符串SystemName {get;私人套装; }
}
}
namespace ConsoleApplication1
{
using System.Text;
using System.Data.Odbc;
using System.Data;
using System.Web;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.
class Program
{
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();
foreach (var usbDevice in usbDevices)
{
string m_pendid;
Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}, USBVersion: {3}, SystemName: {4}",
usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description, usbDevice.usbversion, usbDevice.SystemName);
// m_pendid=catch["usbDevice.DeviceID"];
m_pendid = usbDevice.DeviceID;
Console.WriteLine("Test" + m_pendid);
}
// Console.Write("DeviceID :DeviceID");
Console.Read();
}
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"),
(string)device.GetPropertyValue("USBVersion"),
(string)device.GetPropertyValue("SystemName")
));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description, string usbversion1, string SystemName2)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
this.usbversion = usbversion1;
this.SystemName = SystemName2;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
public string usbversion { get; private set; }
public string SystemName { get; private set; }
}
}
这篇关于获取USB驱动器的硬件ID.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文