這裏是我使用的代碼,序列號以某種方式返回原始與每對字符顛倒(奇怪)和使用Win32_PhysicalMedia給出了不同的結果,如果我作爲用戶或管理員(更奇怪)運行代碼 - Windows用VB 7旗艦版,VS 2008只:
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
參見http://stackoverflow.com/questions/1502812/problem-with-wmi – finnw 2009-10-04 11:12:07