我正在尝试使用 WMI 对我的 C: Drive 进行碎片整理。
我正在运行 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 Code Creator 获得了这段代码,但是当我运行它时,它返回一个异常,说“未找到”。
有没有其他人试过这个?