VB.NET 输出方法参数的帮助
我正在尝试使用 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
我无法理解的是如何获取参数信息(即“VolumeSize”)来自“DefragAnalysis”方法。上面的代码返回“找不到方法错误”。
谢谢你
-编辑 这是当前在 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 代码无论平台如何都可以工作。
注意:我已经使用了“Console.WriteLine”并将其更改为“Debug.WriteLine”以将值输出到立即窗口。
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 not 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有
VolumeSize
属性。当您调用 DefragAnalysis() 时返回函数的状态并输出参数
boolean DefragRecommended
和object DefragAnalysis
。DefragAnalysis 类 包含属性
体积大小
。您应该始终阅读文档进行猜测。
我想推荐一个名为 WMI 代码创建器 v1.0。该工具允许您生成 VBScript、C# 和 VB .NET 代码,这些代码使用 WMI 完成管理任务,例如查询管理数据、执行 WMI 类的方法或使用 WMI 接收事件通知。
我希望这有帮助。 :)
There is no property as
VolumeSize
.When you call DefragAnalysis() it returns the status of the function and outs parameters
boolean DefragRecommended
andobject DefragAnalysis
.DefragAnalysis Class contains property
VolumeSize
.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. : )