Win32_PhysicalMedia SerialNumber属性的问题

时间:2009-08-29 09:06:00

标签: wmi

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

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

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

这是我使用的代码,序列号以某种方式返回原始,每对字符反转(奇怪)并使用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