gpt4 book ai didi

c# - 如何使用 void ManagementObject.InvokeMethod(...) 获取 Win32_Volume 类的 Format 方法的返回值?

转载 作者:太空宇宙 更新时间:2023-11-03 20:19:32 26 4
gpt4 key购买 nike

最佳答案

要直接获得结果,您可以使用 ManagementObject.InvokeMethod Method (String, ManagementBaseObject, InvokeMethodOptions) InvokeMethod 方法的版本。

像这样

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
class Program
{
// This method is used to format the volume.

static void Main(string[] args)
{
try
{
ManagementScope Scope;
Scope = new ManagementScope("\\\\.\\root\\CIMV2", null);

Scope.Connect();
ObjectGetOptions Options = new ObjectGetOptions();
ManagementPath Path = new ManagementPath("Win32_Volume.DeviceID=\"\\\\\\\\?\\\\Volume{178edf63-2039-11e2-8012-005056c00008}\\\\\"");
ManagementObject ClassInstance= new ManagementObject(Scope, Path, Options);
ManagementBaseObject inParams = ClassInstance.GetMethodParameters("Format");


ManagementBaseObject outParams= ClassInstance.InvokeMethod("Format", inParams ,null);
Console.WriteLine("{0,-35} {1,-40}","Return Value",outParams["ReturnValue"]);

}
catch (Exception e)
{
Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
}
Console.WriteLine("Press Enter to exit");
Console.Read();
}
}
}

现在,如果您使用的是 ManagementOperationObserver对于异步执行,可以在ObjectReady中得到结果事件

像这样

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
public class InvokeAsync
{
public InvokeAsync()
{

// Create a results watcher object,
ManagementOperationObserver results = new ManagementOperationObserver();

// Attach handler to events for results and completion
results.ObjectReady += new ObjectReadyEventHandler(this.NewObject);
results.Completed += new CompletedEventHandler(this.Done);

ManagementScope Scope;
Scope = new ManagementScope("\\\\.\\root\\CIMV2", null);

Scope.Connect();
ObjectGetOptions Options = new ObjectGetOptions();
ManagementPath Path = new ManagementPath("Win32_Volume.DeviceID=\"\\\\\\\\?\\\\Volume{178edf63-2039-11e2-8012-005056c00008}\\\\\"");
ManagementObject ClassInstance = new ManagementObject(Scope, Path, Options);
ManagementBaseObject inParams = ClassInstance.GetMethodParameters("Format");


ClassInstance.InvokeMethod(results, "Format", inParams, null);

while (!this.Completed)
{
System.Threading.Thread.Sleep(1000);
}

this.Reset();

}

private bool isCompleted = false;

private void NewObject(object sender,
ObjectReadyEventArgs obj)
{
Console.WriteLine("ReturnValue : {0}", obj.NewObject["ReturnValue"]);
}

private bool Completed
{
get
{
return isCompleted;
}
}

private void Reset()
{
isCompleted = false;
}

private void Done(object sender,
CompletedEventArgs obj)
{
isCompleted = true;
}

public static void Main()
{
InvokeAsync example =
new InvokeAsync();

return;
}

}
}

关于c# - 如何使用 void ManagementObject.InvokeMethod(...) 获取 Win32_Volume 类的 Format 方法的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com