为什么在尝试使用 WMI 时出现 'Invalid namespace' 错误?
Why do I get an 'Invalid namespace' error when attempting to use WMI?
我需要获取硬盘驱动器的序列号作为用户识别的唯一 ID,但是当我尝试访问序列号时程序崩溃了:
An unhandled exception of type 'System.Management.ManagementException' occurred in System.Management.dll
Additional information: Invalid namespace
我环顾四周,似乎没有太多关于这个问题的; one website 提到 'WMI is like Death Valley',我倾向于同意这一点。
错误发生在 moHD.[Get]()
。
为什么会出现此错误,我该如何解决?
代码:
Public Function getSerial(ByVal strDrive As String) As String 'Get HD Serial Number
If strDrive = "" OrElse strDrive Is Nothing Then
strDrive = "C"
End If
Dim moHD As New ManagementObject("Win32_LogicalDisk.DeviceID=""" + strDrive + ":""")
moHD.[Get]()
Return moHD("VolumeSerialNumber").ToString()
End Function
尝试传递 Win32_LogicalDisk
class 所在的命名空间。检查此示例:
Public Function getSerial(ByVal strDrive As String) As String 'Get HD Serial Number
If strDrive = "" OrElse strDrive Is Nothing Then
strDrive = "C"
End If
Dim scope As New ManagementScope("\.\root\cimv2")
Dim path As New ManagementPath("Win32_LogicalDisk.DeviceID=""" + strDrive + ":""")
Dim moHD As New ManagementObject(scope, path, Nothing)
moHD.[Get]()
Return moHD("VolumeSerialNumber").ToString()
End Function
我需要获取硬盘驱动器的序列号作为用户识别的唯一 ID,但是当我尝试访问序列号时程序崩溃了:
An unhandled exception of type 'System.Management.ManagementException' occurred in System.Management.dll
Additional information: Invalid namespace
我环顾四周,似乎没有太多关于这个问题的; one website 提到 'WMI is like Death Valley',我倾向于同意这一点。
错误发生在 moHD.[Get]()
。
为什么会出现此错误,我该如何解决?
代码:
Public Function getSerial(ByVal strDrive As String) As String 'Get HD Serial Number
If strDrive = "" OrElse strDrive Is Nothing Then
strDrive = "C"
End If
Dim moHD As New ManagementObject("Win32_LogicalDisk.DeviceID=""" + strDrive + ":""")
moHD.[Get]()
Return moHD("VolumeSerialNumber").ToString()
End Function
尝试传递 Win32_LogicalDisk
class 所在的命名空间。检查此示例:
Public Function getSerial(ByVal strDrive As String) As String 'Get HD Serial Number
If strDrive = "" OrElse strDrive Is Nothing Then
strDrive = "C"
End If
Dim scope As New ManagementScope("\.\root\cimv2")
Dim path As New ManagementPath("Win32_LogicalDisk.DeviceID=""" + strDrive + ":""")
Dim moHD As New ManagementObject(scope, path, Nothing)
moHD.[Get]()
Return moHD("VolumeSerialNumber").ToString()
End Function