[英]How to get serial number of USB-Stick in C#
如何在C#中獲取USB-Stick或USB-HardDrive的內部序列號?
試試這個:
// add a reference to the System.Management assembly and
// import the System.Management namespace at the top in your "using" statement.
// Then in a method, or on a button click:
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());
}
資料來源: http : //social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/f4447ed3-7e5f-4635-a28a-afff0b620156/
我遇到了Yuval Adam提供的解決方案的問題,因為我試過的每個USB記憶棒在Windows 7上都是空白的。
我通過查看當前對象上的屬性PNPDeviceId解決了這個問題。
例如
currentObject["PNPDeviceID"].ToString();
不確定這是多么有效,但它在我試過的3個USB棒上對我有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.