我正在寻找一种通过按下按钮创建具有当前日期和时间的系统还原点的方法.我试过在网上搜索一个简单的方法,但我还没找到.
我发现这个代码片段来自: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); }