如何以编程方式启用驱动器的系统还原监视?

时间:2012-12-17 09:42:54

标签: delphi wmi delphi-7

我找到了一个code that enables System Restore monitoring,但它适用于C#,我需要将其转换为Delphi。这是代码:

ManagementScope scope = new ManagementScope("\\\\localhost\\root\\default");
ManagementPath path = new ManagementPath("SystemRestore");
ObjectGetOptions options = new ObjectGetOptions();
ManagementClass process = new ManagementClass(scope, path, options);
ManagementBaseObject inParams = process.GetMethodParameters("Enable");
inParams["WaitTillEnabled"] = true;
inParams["Drive"] = osDrive;
ManagementBaseObject outParams = process.InvokeMethod("Enable", inParams, null);

有人可以帮我将上述代码转换为Delphi吗?

1 个答案:

答案 0 :(得分:3)

如果已启用指定驱动器的System Restore监视,则以下函数返回True,否则返回False。输入ADrive参数指定要监视的完整驱动器路径。当此参数是系统驱动器或空字符串时,将监视所有驱动器。此函数不会等待监视在返回之前完全启用。相反,它在启动系统还原服务和过滤器驱动程序后立即返回:

function EnableSystemRestore(const ADrive: string): Boolean;
var
  WbemObject: OleVariant;
  WbemService: OleVariant;
  WbemLocator: OleVariant;
begin;
  Result := False;
  try
    WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
    WbemService := WbemLocator.ConnectServer('localhost', 'root\DEFAULT');
    WbemObject := WbemService.Get('SystemRestore');
    Result := WbemObject.Enable(ADrive) = S_OK;
  except
    on E: EOleException do
      ShowMessage(Format('EOleException %s %x', [E.Message, E.ErrorCode]));
    on E: Exception do
      ShowMessage(E.Classname + ':' + E.Message);
  end;
end;

用法:

procedure TForm1.Button1Click(Sender: TObject);
begin;
  if not EnableSystemRestore('D:\') then
    ShowMessage('Failed!')
  else
    ShowMessage('Succeeded!');
end;