微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何以编程方式创建系统还原点?

我正在寻找一种通过按下按钮创建具有当前日期和时间的系统还原点的方法.我试过在网上搜索一个简单的方法,但我还没找到.

我发现这个代码片段来自:http://msdn.microsoft.com/en-us/library/windows/desktop/aa378847%28v=vs.85%29.aspx,但它是在VB而不是C#,我尝试转换它有点但我不认为我在翻译它做得很好.

'CreateRestorePoint Method of the SystemRestore Class
'Creates a restore point. Specifies the beginning and 
'the ending of a set of changes so that System Restore 
'can create a restore point.This method is the 
'scriptable equivalent of the SRSetRestorePoint function.

Set Args = wscript.Arguments
If Args.Count() > 0 Then
    rpname = Args.item(0)
Else 
    rpname = "Vbscript"
End If

Set obj = Getobject("winmgmts:{impersonationLevel=impersonate}!root/default:SystemRestore")

If (obj.CreateRestorePoint(rpname,100)) = 0 Then
wscript.Echo "Success"
Else 
    wscript.Echo "Failed"
End If

解决方法

这是一个用于创建还原点的VB.NET代码段(找到 here):
Dim restPoint = Getobject("winmgmts:\\.\root\default:Systemrestore")
If restPoint IsNot nothing Then
     If restPoint.CreateRestorePoint("test restore point",100) = 0 Then
         MsgBox("Restore Point created successfully")
    Else
         MsgBox("Could not create restore point!")
     End If
End If

应该很容易“翻译”到C#.

这是从this question开始的C#中的另一个片段:

private void CreateRestorePoint(string description)
{
    ManagementScope oScope = new ManagementScope("\\\\localhost\\root\\default");
    ManagementPath oPath = new ManagementPath("SystemRestore");
    ObjectGetoptions oGetop = new ObjectGetoptions();
    ManagementClass oProcess = new ManagementClass(oScope,oPath,oGetop);

    ManagementBaSEObject oInParams =
         oProcess.getmethodParameters("CreateRestorePoint");
    oInParams["Description"] = description;
    oInParams["RestorePointType"] = 12; // MODIFY_SETTINGS
    oInParams["EventType"] = 100;

    ManagementBaSEObject oOutParams =
         oProcess.InvokeMethod("CreateRestorePoint",oInParams,null); 
}

原文地址:https://www.jb51.cc/csharp/243685.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐