Win32_PhysicalMedia SerialNumber 属性存在问题

发布于 2024-08-03 20:51:52 字数 280 浏览 3 评论 0原文

我编写了以下代码来获取物理介质序列号,但在我的一台计算机中它返回 null。 有人知道问题是什么吗? 谢谢。

var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
foreach( ManagementObject mo in searcher.Get() )
{
    Console.WriteLine("Serial: {0}", mo["SerialNumber"]);
}

I wrote the following code to get the physical media serial number but in one of my computers it returns null instead.
Does anybody know what the problem is?
Thanks.

var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
foreach( ManagementObject mo in searcher.Get() )
{
    Console.WriteLine("Serial: {0}", mo["SerialNumber"]);
}

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

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

发布评论

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

评论(2

宣告ˉ结束 2024-08-10 20:51:52

序列号是可选的,由制造商定义,对于您的设备来说,它要么是空白的,要么不受驱动程序支持。

几乎所有硬盘驱动器都有序列号,但大多数 USB 式闪存棒没有(通常是成本问题)。我想大多数无品牌的 CD/DVD/BD 光盘也将是非序列化的。

The Serial Number is optional, defined by the manufacturer, and for your device it is either blank or unsupported by the driver.

Virtually all hard drives have a serial number, but most USB-style Flash memory sticks do not (generally a cost issue). I would imagine most unbranded CD/DVD/BD discs would also be non-serialized.

云裳 2024-08-10 20:51:52

这是我使用的代码,序列号以某种方式返回原始,每对字符反转(奇怪),如果我以用户或管理员身份运行代码(更奇怪),使用 Win32_PhysicalMedia 会给出不同的结果 - Windows 7 Ultimate,VS 2008 年仅使用 VB:

Function GetHDSerial() As String
    Dim strHDSerial As String = String.Empty
    Dim index As Integer = 0
    Dim Data As String
    Dim Data2 As String
    Dim ndx As Integer

    Dim query As New SelectQuery("Win32_DiskDrive")
    Dim search As New ManagementObjectSearcher(query)
    Dim info As ManagementObject
    Try
        For Each info In search.Get()
            Data = info("SerialNumber")
            Data2 = ""
            For ndx = 1 To Data.Length - 1 Step 2
                Data2 = Data2 & Chr(Val("&H" & Mid(Data, ndx, 2)))
            Next ndx
            Data = String.Empty
            For ndx = 1 To Data2.Length - 1 Step 2
                Data = Data & Mid(Data2, ndx + 1, 1) & Mid(Data2, ndx, 1)
            Next
            Data2 = Data
            If Len(Data) < 8 Then Data2 = "00000000" 'some drives have no s/n
            Data2 = Replace(Data2, " ", "") ' some drives pad spaces in the s/n
            'forget removeable drives
            If InStr(info("MediaType").ToString, "Fixed", CompareMethod.Text) > 0 Then
               strHDSerial = strHDSerial & "Drive " & index.ToString & " SN: " & Data2 & vbCrLf
               index += 1
            End If
        Next
    Catch ex As Exception
        strHDSerial = "Error retrieving SN for Drive " 
        msgbox(index.ToString)
    End Try
    Return strHDSerial
End Function

Here is the code I used, the serial number somehow is returned raw with each pair of chars reversed (strange) and using Win32_PhysicalMedia gave different results if I ran the code as a user or an Administrator(more strange) - Windows 7 Ultimate, VS 2008 using VB only:

Function GetHDSerial() As String
    Dim strHDSerial As String = String.Empty
    Dim index As Integer = 0
    Dim Data As String
    Dim Data2 As String
    Dim ndx As Integer

    Dim query As New SelectQuery("Win32_DiskDrive")
    Dim search As New ManagementObjectSearcher(query)
    Dim info As ManagementObject
    Try
        For Each info In search.Get()
            Data = info("SerialNumber")
            Data2 = ""
            For ndx = 1 To Data.Length - 1 Step 2
                Data2 = Data2 & Chr(Val("&H" & Mid(Data, ndx, 2)))
            Next ndx
            Data = String.Empty
            For ndx = 1 To Data2.Length - 1 Step 2
                Data = Data & Mid(Data2, ndx + 1, 1) & Mid(Data2, ndx, 1)
            Next
            Data2 = Data
            If Len(Data) < 8 Then Data2 = "00000000" 'some drives have no s/n
            Data2 = Replace(Data2, " ", "") ' some drives pad spaces in the s/n
            'forget removeable drives
            If InStr(info("MediaType").ToString, "Fixed", CompareMethod.Text) > 0 Then
               strHDSerial = strHDSerial & "Drive " & index.ToString & " SN: " & Data2 & vbCrLf
               index += 1
            End If
        Next
    Catch ex As Exception
        strHDSerial = "Error retrieving SN for Drive " 
        msgbox(index.ToString)
    End Try
    Return strHDSerial
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文