如何通过编程创建系统还原点?

6
我正在寻找一种通过按下按钮来创建具有当前日期和时间的系统还原点的方法。我已经尝试在网上搜索一个简单的方法来实现这个功能,但是我还没有找到合适的方法。
我在http://msdn.microsoft.com/en-us/library/windows/desktop/aa378847%28v=vs.85%29.aspx找到了这段代码片段,但它是用VB而不是C#编写的。我尝试将其转换为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, 0, 100)) = 0 Then
wscript.Echo "Success"
Else 
    wscript.Echo "Failed"
End If

请参阅此问题:https://dev59.com/A0XRa4cB1Zd3GeqPogw2 - M4N
3个回答

8

以下是创建还原点的VB.NET代码片段(取自此处):

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

应该很容易将其“翻译”为C#。

这里是另一个摘自这个问题的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); 
}

问题要求使用C#进行回答。无论如何,这是一个好答案。 - daryal
我之前尝试过@M4N提供的方法,但是出现了许多缺失程序集引用错误。我不确定需要添加哪些引用。 - Boundinashes6
@Boundinashes6:你可能需要添加对 System.Management 的引用(请参见此处:http://msdn.microsoft.com/en-us/library/system.management.managementscope.aspx)。 - M4N
这不会创建任何还原点,但仍然可以成功执行。有什么建议吗? - Vilas Joshi

5

虽然有些晚,但我改进了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);
    }
}

例子:

CreateRestorePoint("Example Restore Point", EventType.BeginSystemChange, RestorePointType.ModifySettings);

请提供一些解释,以便用户了解代码的含义和如何使用,而不仅仅是提供代码。 - CodeChanger
4
我认为由于内部代码摘要,它应该是不言自明的。但我添加了一个例子。 - sombra

1
var restPoint = GetObject(@"winmgmts:\\.\root\default:Systemrestore");
if(restPoint!=null)
{
    if(restPoint.CreateRestorePoint("", 0, 100) == 0)
    {
        //do something
    }
    else
    {
         //do something
    }
}

感谢您的回复@David Brabant,这在此时给我一个未被识别的转义序列错误。变量restPoint = GetObject("winmgmts:\.\root\default:Systemrestore"); - Boundinashes6
答案来自youhannesdedope。编辑以解决转义问题。 - David Brabant
好的,那个错误已经解决了,但现在我收到了以下错误:当前上下文中不存在“GetObject”的名称。 - Boundinashes6
1
ydd1987,仅有代码的答案通常并不有帮助,因为它们无法解释答案的要点。请稍微详细地阐述一下。 - Jacobm001
@Boundinashes6 添加对 Microsoft.VisualBasic 的引用,然后使用 Microsoft.VisualBasic.Interaction.GetObject("winmgmts:\\.\root\default:Systemrestore") - J. Scott Elblein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接