2011-02-19 65 views
0

我想打印(文本文件)的碎片的信息通過Win32_Volume類使用DefragAnalysis方法給予,並拿出了下面的VB.NET代碼: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代碼造物主執行什麼當前工作:

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代碼無論在哪種平臺上都可以工作。

注意:我已經玩過「Console.WriteLine」並將其改爲「Debug.WriteLine」以將值輸出到即時窗口。

回答

0

沒有財產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完成管理任務(如查詢管理數據,執行WMI類中的方法或使用WMI接收事件通知)的VBScript,C#和VB .NET代碼。

我希望這會有所幫助。 :)

+0

謝謝你幫助我:) 雖然我有最奇怪的事情,但直接粘貼到我的VB.NET應用程序中的WMI Code Creator(VB.NET)中生成的代碼將返回零值,但是當直接從WMI Code Creator(執行代碼)執行時,它相應地起作用。有什麼建議麼? – user622622 2011-02-19 07:24:40