VB.NET Help with outputting Method Parameters
I am trying to print (to a text file) the fragmentation information give by Win32_Volume class using the DefragAnalysis method and have come up with the following VB.NET code:
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
The thing i cannot get my head around is how to get the parameter info (i.e. "VolumeSize") from the method "DefragAnalysis". The above code returns an "Method not found error".
Thank you
-Edit This is what currently works when executed in 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 Output: Volume size: 40857.9960763451MB Cluster size: 0.003906249998336MB You do not need to defragment this volume.
However executing this in Visual Studio returns the below: Volume size: MB Cluster size: MB You do n开发者_运维问答ot need to defragment this volume.
The point here is though it does NOT work under Windows Server 2008 R2, but does work under Windows Server 2003 (when executed in Visual Studio), WMI Code will work regardless of platform.
NB: i have played with the "Console.WriteLine" and changed it to "Debug.WriteLine" to output value to immediate window.
There is no property as VolumeSize
.
When you call DefragAnalysis() it returns the status of the function and outs parameters boolean DefragRecommended
and object DefragAnalysis
.
DefragAnalysis Class contains property VolumeSize
.
Console.WriteLine("DefragRecommended: {0}", outParams("DefragRecommended"))
Console.WriteLine("VolumeSize: {0}", outParams("DefragAnalysis")("VolumeSize"))
Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))
You should always read the documentation instead of making a guess.
I would like to suggest a nice tool called WMI Code Creator v1.0. This tool tool allows you to generate VBScript, C#, and VB .NET code that uses WMI to complete a management task such as querying for management data, executing a method from a WMI class, or receiving event notifications using WMI.
I hope this helps. : )
精彩评论