我試圖使用WMI整理我的C:驅動器。在C#中找不到WMI碎片整理方法
我正在運行Windows 7 Pro x64。
Console.WriteLine(SMARTManager.Instance.SMARTHDD.Defrag("C:", ref ERR));
功能:
public string Defrag(string a_DriveName, ref string ERR)
{
try
{
ManagementObject classInstance =
new ManagementObject("root\\CIMV2",
String.Format("Win32_Volume.DeviceID='{0}'", a_DriveName),
null);
// Obtain in-parameters for the method
ManagementBaseObject inParams =
classInstance.GetMethodParameters("Defrag");
// Add the input parameters.
inParams["Force"] = true;
// Execute the method and obtain the return values.
ManagementBaseObject outParams =
classInstance.InvokeMethod("Defrag", inParams, null);
// List outParams
string callback = "Out parameters:\n" + "ReturnValue: " + outParams["ReturnValue"];
return callback;
}
catch (ManagementException err)
{
ERR = "An error occurred while trying to execute the WMI method: " + err.Message;
}
return null;
}
我從WMI代碼造物主這個代碼,但是當我運行它,它會返回一個exeption說 「未找到」。
有沒有其他人試過這個?
什麼操作系統,你運行這個樣本代碼? WMI提供程序因不同的Windows版本而異。 – Dai 2012-08-08 17:31:55
Windows 7 Pro x64 – 2012-08-08 17:48:42