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); 
}

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...