How to create a system restore point programmatically?

后端 未结 3 1173
迷失自我
迷失自我 2020-12-17 01:02

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

相关标签:
3条回答
  • 2020-12-17 02:01

    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); 
    }
    
    0 讨论(0)
  • 2020-12-17 02:02
    var restPoint = GetObject(@"winmgmts:\\.\root\default:Systemrestore");
    if(restPoint!=null)
    {
        if(restPoint.CreateRestorePoint("", 0, 100) == 0)
        {
            //do something
        }
        else
        {
             //do something
        }
    }
    
    0 讨论(0)
  • 2020-12-17 02:05

    Late, but I improved the answer from M4N...

    /// <summary>
    ///     The type of event. For more information, see <see cref="CreateRestorePoint"/>.
    /// </summary>
    public enum EventType
    {
        /// <summary>
        ///     A system change has begun. A subsequent nested call does not create a new restore
        ///     point.
        ///     <para>
        ///         Subsequent calls must use <see cref="EventType.EndNestedSystemChange"/>, not
        ///         <see cref="EventType.EndSystemChange"/>.
        ///     </para>
        /// </summary>
        BeginNestedSystemChange = 0x66,
    
        /// <summary>
        ///     A system change has begun.
        /// </summary>
        BeginSystemChange = 0x64,
    
        /// <summary>
        ///     A system change has ended.
        /// </summary>
        EndNestedSystemChange = 0x67,
    
        /// <summary>
        ///     A system change has ended.
        /// </summary>
        EndSystemChange = 0x65
    }
    
    /// <summary>
    ///     The type of restore point. For more information, see <see cref="CreateRestorePoint"/>.
    /// </summary>
    public enum RestorePointType
    {
        /// <summary>
        ///     An application has been installed.
        /// </summary>
        ApplicationInstall = 0x0,
    
        /// <summary>
        ///     An application has been uninstalled.
        /// </summary>
        ApplicationUninstall = 0x1,
    
        /// <summary>
        ///     An application needs to delete the restore point it created. For example, an
        ///     application would use this flag when a user cancels an installation. 
        /// </summary>
        CancelledOperation = 0xd,
    
        /// <summary>
        ///     A device driver has been installed.
        /// </summary>
        DeviceDriverInstall = 0xa,
    
        /// <summary>
        ///     An application has had features added or removed.
        /// </summary>
        ModifySettings = 0xc
    }
    
    /// <summary>
    ///     Creates a restore point on the local system.
    /// </summary>
    /// <param name="description">
    ///     The description to be displayed so the user can easily identify a restore point.
    /// </param>
    /// <param name="eventType">
    ///     The type of event.
    /// </param>
    /// <param name="restorePointType">
    ///     The type of restore point. 
    /// </param>
    /// <exception cref="ManagementException">
    ///     Access denied.
    /// </exception>
    public static void CreateRestorePoint(string description, EventType eventType, RestorePointType restorePointType)
    {
        var mScope = new ManagementScope("\\\\localhost\\root\\default");
        var mPath = new ManagementPath("SystemRestore");
        var options = new ObjectGetOptions();
        using (var mClass = new ManagementClass(mScope, mPath, options))
        using (var parameters = mClass.GetMethodParameters("CreateRestorePoint"))
        {
            parameters["Description"] = description;
            parameters["EventType"] = (int)eventType;
            parameters["RestorePointType"] = (int)restorePointType;
            mClass.InvokeMethod("CreateRestorePoint", parameters, null);
        }
    }
    

    Example:

    CreateRestorePoint("Example Restore Point", EventType.BeginSystemChange, RestorePointType.ModifySettings);
    
    0 讨论(0)
提交回复
热议问题