.Net 相当于 VB6 的 IsObject 用于 WMI 查询
.Net equivalent of VB6's IsObject for WMI queries
我确定我不是第一个遇到这种情况的人。搜索 "VB.Net isobject" returns 什么都没有,所以答案可能太明显了,不值得发布。
我的代码:
Dim colItems As Object
colItems = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)
Dim objItem As Object
For Each objItem In colItems
If isObject(objItem) Then
If objItem.StatusCode = 0 Then
IsOnline = True
colItems = Nothing
Exit Function
End If
End If
Next
错误:
'IsObject' is not declared. It may be inaccessible due to its protection level.
我认为我需要的是
If Not objItem Is Nothing Then
对吗?
没有直接等价物。 IsObject
在 VB6(和 is used in VBA)中用于确定 Variant
是否包含对象引用。
由于 Object
是 VB6 Variant
的 VB.NET 后继者,甚至内置的基本类型(整数、字符串等)都派生自 Object
, IsObject
在 VB.NET 中有点毫无意义。
最接近的等价物可能是 IsReference。
如前所述,IsObject()
在 .NET 中的用处明显较小,但鉴于上下文,您似乎想要测试 Nothing
/ null。在这种情况下,这些将起作用
If Not objItem Is Nothing Then ...
' or
If objItem IsNot Nothing Then ...
更重要的是代码不会在Option Strict
下编译:
Dim colItems As Object
colItems = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)
Object
没有 ExecQuery
方法,因此代码需要后期绑定; objItem.StatusCode
也一样。
代码通常看起来可能源自脚本。当 WMI
有一个很好的 NET 包装器时,不需要使用 COM 接口和处理 Object
和后期绑定。我不知道你在查询什么,这将获取 BIOS 的序列号:
Option Strict On
Imports System.Management
Public Class WMI
Friend Shared Function ExecWMIQuery(wmiclass As String, queryItem As String) As String
Dim retVal As String = ""
Dim query = String.Format("SELECT {0} FROM {1}", queryItem, wmiclass)
Using searcher As New ManagementObjectSearcher(query)
For Each item As ManagementObject In searcher.Get
Dim p = item.Properties(queryItem)
If (p IsNot Nothing) AndAlso (p.Value IsNot Nothing) Then
retVal = p.Value.ToString
' should be nothing else...
Exit For
End If
Next
End Using
Return retVal
End Function
End Class
用法:
Dim mySerial = WMI.ExecWMIQuery("Win32_Bios", "SerialNumber")
Console.WriteLine(mySerial)
我倾向于谨慎使用 WMI,因为您通常会受到制造商选择包含或省略的内容的支配,因此需要检查 Nothing
等等。
更重要的一点是 System.Management
公开了类型化对象和集合,因此您不必声明所有内容 As Object
并且可以使用 Option Strict
来防止更糟糕的事情发生。
另请参阅:
- ManagementObject Class
- WMI Classes ...所有 类 及其提供的内容的起点。
我确定我不是第一个遇到这种情况的人。搜索 "VB.Net isobject" returns 什么都没有,所以答案可能太明显了,不值得发布。
我的代码:
Dim colItems As Object
colItems = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)
Dim objItem As Object
For Each objItem In colItems
If isObject(objItem) Then
If objItem.StatusCode = 0 Then
IsOnline = True
colItems = Nothing
Exit Function
End If
End If
Next
错误:
'IsObject' is not declared. It may be inaccessible due to its protection level.
我认为我需要的是
If Not objItem Is Nothing Then
对吗?
没有直接等价物。 IsObject
在 VB6(和 is used in VBA)中用于确定 Variant
是否包含对象引用。
由于 Object
是 VB6 Variant
的 VB.NET 后继者,甚至内置的基本类型(整数、字符串等)都派生自 Object
, IsObject
在 VB.NET 中有点毫无意义。
最接近的等价物可能是 IsReference。
如前所述,IsObject()
在 .NET 中的用处明显较小,但鉴于上下文,您似乎想要测试 Nothing
/ null。在这种情况下,这些将起作用
If Not objItem Is Nothing Then ...
' or
If objItem IsNot Nothing Then ...
更重要的是代码不会在Option Strict
下编译:
Dim colItems As Object
colItems = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)
Object
没有 ExecQuery
方法,因此代码需要后期绑定; objItem.StatusCode
也一样。
代码通常看起来可能源自脚本。当 WMI
有一个很好的 NET 包装器时,不需要使用 COM 接口和处理 Object
和后期绑定。我不知道你在查询什么,这将获取 BIOS 的序列号:
Option Strict On
Imports System.Management
Public Class WMI
Friend Shared Function ExecWMIQuery(wmiclass As String, queryItem As String) As String
Dim retVal As String = ""
Dim query = String.Format("SELECT {0} FROM {1}", queryItem, wmiclass)
Using searcher As New ManagementObjectSearcher(query)
For Each item As ManagementObject In searcher.Get
Dim p = item.Properties(queryItem)
If (p IsNot Nothing) AndAlso (p.Value IsNot Nothing) Then
retVal = p.Value.ToString
' should be nothing else...
Exit For
End If
Next
End Using
Return retVal
End Function
End Class
用法:
Dim mySerial = WMI.ExecWMIQuery("Win32_Bios", "SerialNumber")
Console.WriteLine(mySerial)
我倾向于谨慎使用 WMI,因为您通常会受到制造商选择包含或省略的内容的支配,因此需要检查 Nothing
等等。
更重要的一点是 System.Management
公开了类型化对象和集合,因此您不必声明所有内容 As Object
并且可以使用 Option Strict
来防止更糟糕的事情发生。
另请参阅:
- ManagementObject Class
- WMI Classes ...所有 类 及其提供的内容的起点。