gpt4 book ai didi

c# - 如何在 C# 中从 USB 闪存驱动器获取 VID/PID?

转载 作者:太空狗 更新时间:2023-10-29 20:34:41 24 4
gpt4 key购买 nike

我需要对所有驱动器执行检查,看看是否有任何 VID/PID 与特定驱动器匹配,如果匹配,我需要获取该闪存驱动器的驱动器号。感谢大家!

最佳答案

WMI 应该能够处理这个...

您必须添加对 System.Management dll 的引用,并且您需要:“使用系统管理;”行...请参阅底部的链接以获取屏幕截图,更详尽的解释...

using System.Management;
// Get all the disk drives

ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

// Loop through each object (disk) retrieved by WMI

foreach (ManagementObject moDisk in mosDisks.Get())

{

// Add the HDD to the list (use the Model field as the item's caption)

cmbHdd.Items.Add(moDisk["Model"].ToString());

}


private void cmbHdd_SelectedIndexChanged(object sender, EventArgs e)

{

// Get all the disk drives from WMI that match the Model name selected in the ComboBox

ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Model = '" + cmbHdd.SelectedItem + "'");

// Loop through the drives retrieved, although it should normally be only one loop going on here

foreach (ManagementObject moDisk in mosDisks.Get())

{

// Set all the fields to the appropriate values

lblType.Text = "Type: " + moDisk["MediaType"].ToString();

lblModel.Text = "Model: " + moDisk["Model"].ToString();

lblSerial.Text = "Serial: " + moDisk["SerialNumber"].ToString();

lblInterface.Text = "Interface: " + moDisk["InterfaceType"].ToString();

// The capacity in gigabytes is easily calculated

lblCapacity.Text = "Capacity: " + moDisk["Size"].ToString() + " bytes (" + Math.Round(((((double)Convert.ToDouble(moDisk["Size"]) / 1024) / 1024) / 1024), 2) + " GB)";

lblPartitions.Text = "Partitions: " + moDisk["Partitions"].ToString();

lblSignature.Text = "Signature: " + moDisk["Signature"].ToString();

lblFirmware.Text = "Firmware: " + moDisk["FirmwareRevision"].ToString();

lblCylinders.Text = "Cylinders: " + moDisk["TotalCylinders"].ToString();

lblSectors.Text = "Sectors: " + moDisk["TotalSectors"].ToString();

lblHeads.Text = "Heads: " + moDisk["TotalHeads"].ToString();

lblTracks.Text = "Tracks: " + moDisk["TotalTracks"].ToString();

lblBytesPerSect.Text = "Bytes per Sector: " + moDisk["BytesPerSector"].ToString();

lblSectorsPerTrack.Text = "Sectors per Track: " + moDisk["SectorsPerTrack"].ToString();

lblTracksPerCyl.Text = "Tracks per Cylinder: " + moDisk["TracksPerCylinder"].ToString();

}

}

来自 MSDN CIM_DiskDrive 的 win32 类具有以下参数:

*看起来好像“DeviceID”就是你想要的...

class Win32_DiskDrive : CIM_DiskDrive
{
uint16 Availability;
uint32 BytesPerSector;
uint16 Capabilities[];
string CapabilityDescriptions[];
string Caption;
string CompressionMethod;
uint32 ConfigManagerErrorCode;
boolean ConfigManagerUserConfig;
string CreationClassName;
uint64 DefaultBlockSize;
string Description;
string DeviceID;
boolean ErrorCleared;
string ErrorDescription;
string ErrorMethodology;
string FirmwareRevision;
uint32 Index;
datetime InstallDate;
string InterfaceType;
uint32 LastErrorCode;
string Manufacturer;
uint64 MaxBlockSize;
uint64 MaxMediaSize;
boolean MediaLoaded;
string MediaType;
uint64 MinBlockSize;
string Model;
string Name;
boolean NeedsCleaning;
uint32 NumberOfMediaSupported;
uint32 Partitions;
string PNPDeviceID;
uint16 PowerManagementCapabilities[];
boolean PowerManagementSupported;
uint32 SCSIBus;
uint16 SCSILogicalUnit;
uint16 SCSIPort;
uint16 SCSITargetId;
uint32 SectorsPerTrack;
string SerialNumber;
uint32 Signature;
uint64 Size;
string Status;
uint16 StatusInfo;
string SystemCreationClassName;
string SystemName;
uint64 TotalCylinders;
uint32 TotalHeads;
uint64 TotalSectors;
uint64 TotalTracks;
uint32 TracksPerCylinder;
};

代码的顶部部分取自:

http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html

关于c# - 如何在 C# 中从 USB 闪存驱动器获取 VID/PID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10057800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com