-1

我找到了一个启用系统还原监控的代码,但它适用于 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 ?

4

1 回答 1

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;
于 2012-12-17T11:36:29.770 回答