此代码中报告了奇怪的错误,请解释一下?

时间:2011-08-01 10:09:02

标签: vb.net wmi

我把这些代码放在一些其他样本中,我收到一个我无法理解的错误。在下面的代码中的这一行,在Observer一词上,

Dim Results As ManagementObjectCollection = Worker.Get(Observer)

我收到错误 “类型'System.Management.ManagementOperationObserver'的值无法转换为'整数'”

有人可以解释一下这意味着什么吗?

ManagementObjectSearcher.Get()有两个签名,一个没有参数,另一个有一个参数,一个用于异步操作的ManagementOperationObserver。这就是我提供的,但错误表明涉及整数的转换?

    Public Shared Sub WMIDriveDetectionASYNC(ByVal args As String())

        Dim Observer As New ManagementOperationObserver()
        Dim completionHandler As New MyHandler()
        AddHandler Observer.Completed, AddressOf completionHandler.Done

        Dim Machine = "192.168.0.15"
        Dim Scope = New ManagementScope("\\" & Machine & "\root\cimv2")
        Dim QueryString = "select Name, Size, FreeSpace from Win32_LogicalDisk where DriveType=3"
        Dim Query = New ObjectQuery(QueryString)
        Dim Worker = New ManagementObjectSearcher(Scope, Query)
        Dim Results As ManagementObjectCollection = Worker.Get(Observer) 'use parameter to make async 

        For Each item As ManagementObject In Results
            Console.WriteLine("{0} {2} {1}", item("Name"), item("FreeSpace"), item("Size"))
            Dim FullSpace As Long = (CLng(item("Size")) - CLng(item("FreeSpace"))) \ 1000000
            Console.WriteLine(FullSpace)
        Next

    End Sub

Public Class MyHandler
        Private _isComplete As Boolean = False

        Public Sub Done(sender As Object, e As CompletedEventArgs)
            _isComplete = True
        End Sub 'Done

        Public ReadOnly Property IsComplete() As Boolean
            Get
                Return _isComplete
            End Get
        End Property

End Class

感谢您的任何建议!

1 个答案:

答案 0 :(得分:2)

我认为使用引用类型来获取结果并将其作为参数放在您发送的对象中。所以我认为它只需要看起来像:

Worker.Get(Observer)

而不是尝试将something =设置为,因为它不是返回值的函数。

然后使用您连接到对象的事件来处理您找到的项目所需的任何内容。