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

相关文章

在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题。比...
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL)。 业务流程:选择照片...
var array = new byte[4]; var i = Encoding.UTF8.GetBytes(100.ToString("x2"));//...
其实很简单,因为Combox的Item是一个K/V的object,那么就可以把它的items转换成IEnumerable<Dic...
把.net4.6安装包打包进安装程序。 关键脚本如下: 头部引用字符串对比库 !include "WordFunc....
项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.NetʿreeSp...