2012-05-25 97 views
6

我正在尋找一種通過按下按鈕來創建系統還原點以及當前日期和時間的方法。我試過在網上搜索一個簡單的方法來做到這一點,但我還沒有找到。如何以編程方式創建系統還原點?

我發現這個代碼片段來自: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, 0, 100)) = 0 Then 
wscript.Echo "Success" 
Else 
    wscript.Echo "Failed" 
End If 
+0

另請參閱此問題:http://stackoverflow.com/questions/32845/creating-system-restore-points-thoughts – M4N

回答

8

這裏有一個VB.NET代碼段創建一個還原點(發現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 

應該很容易 「翻譯」 爲C#。

下面是在C#中的另一個片段從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

問題要求使用c#來回答。無論如何,答案很好。 – daryal

+0

我試過一個以前的@ M4N,它給了我一堆缺少的程序集引用錯誤。我不確定我需要添加哪些。 – Boundinashes6

+0

@ Boundinashes6:您可能需要添加對System.Management的引用(請參閱http://msdn.microsoft.com/en-us/library/system.management.managementscope.aspx) – M4N

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

感謝您的回覆@大衛布拉班特,這是給我一個無法識別的轉義序列錯誤在這一點 var restPoint = GetObject(「winmgmts:\\。\ root \ default:Systemrestore」); – Boundinashes6

+0

答案來自youhannesdedope。編輯它來解決逃生問題。 –

+0

確定錯誤消失了,但現在我收到以下錯誤:名稱'GetObject'在當前上下文中不存在 – Boundinashes6

2

晚了,但我提高了從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); 
+0

請提供一些解釋來獲取用戶理解的代碼以及如何使用,而不僅僅是提供代碼。 – CodeChanger

+2

我認爲這是不言而喻的,因爲內部代碼摘要。但我增加了一個例子。 – Si13n7