【问题标题】:VB.NET Help with outputting Method ParametersVB.NET 帮助输出方法参数
【发布时间】:2011-02-19 02:51:42
【问题描述】:

我正在尝试使用 DefragAnalysis 方法打印(到文本文件)Win32_Volume 类提供的碎片信息,并提出了以下 VB.NET 代码:

Dim objReader As StreamWriter
        objReader = New StreamWriter(FolderBrowserDialog.SelectedPath + "\FragInfo" + "_" + CreationDate + ".txt")

        Dim colItemsFragInfo As New ManagementObjectSearcher("root\CIMV2", "Select * from Win32_Volume where DriveType = 3")

        For Each queryObj As ManagementObject In colItemsFragInfo.Get()
            objReader.WriteLine("Analyzing volume " + queryObj("DriveLetter"))

            Dim InParams As ManagementBaseObject = queryObj.GetMethodParameters("DefragAnalysis")
            Dim OutParams As ManagementBaseObject = queryObj.InvokeMethod("DefragAnalysis", InParams, Nothing)

            MsgBox(OutParams("VolumeSize"))
            objReader.WriteLine(" Volume size: " + OutParams("VolumeSize"))
        Next

        objReader.Close()

    Catch err As ManagementException
        MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)
    End Try

我无法理解的是如何从“DefragAnalysis”方法中获取参数信息(即“VolumeSize”)。上面的代码返回“方法未找到错误”。

谢谢

-编辑 这是当前在 WMI Code Creator 中执行时的工作原理:

Imports System
Imports System.Management
Imports System.Windows.Forms

Namespace WMISample

    Public Class MyWMIQuery

        Public Overloads Shared Function Main() As Integer

            Try

            Dim colItemsVolInfo As New ManagementObjectSearcher("root\CIMV2", "Select * from Win32_Volume where DriveType = '3'")

            For Each queryObj As ManagementObject In colItemsVolInfo.Get()

                Dim OutParams As ManagementBaseObject = queryObj.InvokeMethod("DefragAnalysis", Nothing, Nothing)
                Console.WriteLine(" Volume size: {0}MB", Math.Round(OutParams("DefragAnalysis")("VolumeSize")) * (9.53674316 * 10 ^ -7))
                Console.WriteLine(" Cluster size: {0}MB", Math.Round(OutParams("DefragAnalysis")("ClusterSize")) * (9.53674316 * 10 ^ -7))

                If OutParams("DefragRecommended") = True Then
                    Console.WriteLine("You should defragment this volume.")
                Else
                    Console.WriteLine("You do not need to defragment this volume.")
                End If
                    Next

        Catch err As ManagementException
            MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)

        End Try
        End Function
    End Class
End Namespace

WMI 输出: 卷大小:40857.9960763451MB 集群大小:0.003906249998336MB 您无需对此卷进行碎片整理。

但是在 Visual Studio 中执行此操作会返回以下内容: 卷大小:MB 集群大小:MB 您无需对此卷进行碎片整理。

这里的重点是虽然它在 Windows Server 2008 R2 下不工作,但在 Windows Server 2003 下工作(在 Visual Studio 中执行时),WMI Code 将在任何平台上工作。

注意:我玩过“Console.WriteLine”并将其更改为“Debug.WriteLine”以将值输出到即时窗口。

【问题讨论】:

    标签: vb.net methods parameters wmi


    【解决方案1】:

    没有VolumeSize 的属性。

    当您调用DefragAnalysis() 时,它会返回函数的状态并输出参数boolean DefragRecommendedobject DefragAnalysis

    DefragAnalysis Class 包含属性VolumeSize

    Console.WriteLine("DefragRecommended: {0}", outParams("DefragRecommended"))
    Console.WriteLine("VolumeSize: {0}", outParams("DefragAnalysis")("VolumeSize"))
    Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))
    

    您应该始终阅读documentation 而不是猜测。

    我想推荐一个名为WMI Code Creator v1.0 的好工具。此工具工具允许您生成使用 WMI 来完成管理任务的 VBScript、C# 和 VB .NET 代码,例如查询管理数据、执行 WMI 类中的方法或使用 WMI 接收事件通知。

    我希望这会有所帮助。 :)

    【讨论】:

    • 谢谢你帮了我:) 我有最奇怪的事情发生了,在 WMI Code Creator (VB.NET) 中生成的代码,直接粘贴到我的 VB.NET 应用程序将返回零值,但是当直接从 WMI Code Creator(执行代码)执行时,它会相应地运行。有什么建议吗?
    • @user622622 你说的是同一个代码吗?可以贴出 WMI Code Creator 生成的代码吗?
    • 知道了。抱歉,我完全忽略了操作系统是 x64 并且被构建为导致问题的 x86 的事实。谢谢你的帮助:)
    猜你喜欢
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 2011-09-28
    • 2011-10-22
    • 2011-11-24
    相关资源
    最近更新 更多