如何在C#中获取USB-Stick的序列号

发布于 2024-07-12 03:06:41 字数 38 浏览 7 评论 0原文

如何在 C# 中获取 USB 棒或 USB 硬盘的内部序列号?

How do I get the internal serial number of a USB-Stick or USB-HardDrive in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二手情话 2024-07-19 03:06:41

试试这个:

// 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/

Try this:

// 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());
}

Source: http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/f4447ed3-7e5f-4635-a28a-afff0b620156/

鹿! 2024-07-19 03:06:41

描述了使用 Win32 的解决方案这里

编辑:原始链接似乎丢失了。 上面是一个缓存副本,作者还在VB.Net中编写了一些示例代码,即 仍然在线

A solution using Win32 is described here

Edit: the original link seems to have gone missing. The above is a cached copy, and the author also wrote some sample code in VB.Net which is still online here.

骑趴 2024-07-19 03:06:41

我对 Yuval Adam 提供的解决方案遇到了问题,因为我尝试的每个 USB 记忆棒在 Windows 7 上都返回空白。

我通过查看当前对象上的 PNPDeviceId 属性解决了这个问题。

例如,

currentObject["PNPDeviceID"].ToString();

不确定这有多有效,但它在我尝试过的 3 个 USB 记忆棒上对我有效

I had problems with the solution offered by Yuval Adam as every USB stick I tried return blank on windows 7.

I solved this by just looking at the property PNPDeviceId on the current object.

E.g.

currentObject["PNPDeviceID"].ToString();

Not sure how valid this is but it worked for me on the 3 USB Sticks I tried

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文