ManagementObjectSearcher theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
foreach (ManagementObject currentObject in theSearcher.Get())
{
ManagementObject theSerialNumberObjectQuery = new ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'");
MessageBox.Show(theSerialNumberObjectQuery["SerialNumber"].ToString());
}
这段代码返回USB驱动器的序列号,但我需要使用驱动器号或驱动器路径(针对特定驱动器)查找序列号
答案 0 :(得分:0)
我看到获取序列号的唯一方法是使用Win32(PInvoke):
class Program
{
static void Main(string[] args)
{
var volumeInfo = VolumeInformation.GetVolumeInformation("c");
}
}
[DebuggerDisplay("{ Volume,nq} ({ Name,nq })")]
class VolumeInformation
{
#region Fields
private const int BufferLength = 256;
#endregion
#region Properties
public string Volume
{
get;
}
public string Name
{
get;
}
public uint SerialNumber
{
get;
}
public string SystemName
{
get;
}
#endregion
#region Constructors
private VolumeInformation(string volume, string name, uint serialNumber, string systemName)
{
Volume = volume;
Name = name;
SerialNumber = serialNumber;
SystemName = systemName;
}
#endregion
#region Methods
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool GetVolumeInformation(string letter, StringBuilder name, uint nameSize, out uint serialNumber, out uint serialNumberLength, out uint flags, StringBuilder systemName, uint systemNameSize);
public static VolumeInformation GetVolumeInformation(string volume)
{
var name = new StringBuilder(BufferLength);
var systemName = new StringBuilder(BufferLength);
var serialNumber = 0u;
var serialNumberLength = 0u;
var flags = 0u;
volume = (volume ?? String.Empty).Trim();
if(volume.Length == 1)
{
volume = $"{volume}:\\";
}
if(!volume.EndsWith(@"\"))
{
volume = $"{volume}\\";
}
if (GetVolumeInformation(volume, name, BufferLength, out serialNumber, out serialNumberLength, out flags, systemName, BufferLength))
{
return new VolumeInformation(volume, name.ToString(), serialNumber, systemName.ToString());
}
else
{
return null;
}
}
#endregion
}
我没有填充标志信息,但从Win32返回的标志中可以看出。