How to create a system restore point programmatically?

backup, c#, restore-points, windows

Solution

Here's a VB.NET snippet to create a restore point (found here):

Dim restPoint = GetObject("winmgmts:\\.\root\default:Systemrestore")
If restPoint IsNot Nothing Then
     If restPoint.CreateRestorePoint("test restore point", 0, 100) = 0 Then
         MsgBox("Restore Point created successfully")
    Else
         MsgBox("Could not create restore point!")
     End If
End If

Should be easy to "translate" to C#.

And here's another snippet in C# taken from this question:

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

Problem

I'm looking for a way to create a system restore point with the current date and time by pressing a button. I've tried searching the web for a simple way to do this but I haven't found one yet. I found this code snippet from: http://msdn.microsoft.com/en-us/library/windows/desktop/aa378847%28v=vs.85%29.aspx but it is in VB and not C#, I tried converting it a bit but I don't think i'm doing a great job of translating it. ``` '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, 0, 100)) = 0 Then wscript.Echo "Success" Else wscript.Echo "Failed" End If ```

Original source

Related problems