2012-05-25 12 views
6

मैं एक बटन दबाकर वर्तमान दिनांक और समय के साथ एक सिस्टम पुनर्स्थापना बिंदु बनाने के लिए एक रास्ता तलाश रहा हूँ। मैंने ऐसा करने के लिए एक सरल तरीके से वेब खोजने की कोशिश की है लेकिन मुझे अभी तक कोई नहीं मिला है।प्रोग्राम व्यवस्थित रूप से सिस्टम पुनर्स्थापना बिंदु कैसे बनाएं?

मुझे यह कोड स्निपेट मिला: http://msdn.microsoft.com/en-us/library/windows/desktop/aa378847%28v=vs.85%29.aspx लेकिन यह वीबी में है और सी # नहीं, मैंने इसे थोड़ा बदलने की कोशिश की लेकिन मुझे नहीं लगता कि मैं इसका अनुवाद करने का एक अच्छा काम कर रहा हूं।

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 

सी # करने के लिए "का अनुवाद" करने के लिए आसान होना चाहिए:

'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

यहाँ एक पुनर्स्थापना बिंदु (here पाया जाता है) बनाने के लिए एक VB.NET टुकड़ा है।

और यहाँ 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

मैंने कोशिश की कि एक पिछले @ एम 4 एन, यह मुझे लापता असेंबली संदर्भ त्रुटियों का एक गुच्छा देता है। मुझे यकीन नहीं है कि मुझे कौन सा जोड़ना है। – Boundinashes6

+0

@ बाउंडिनैश 6: आपको शायद सिस्टम के लिए एक संदर्भ जोड़ना होगा। प्रबंधन (यहां देखें: 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

संबंधित मुद्दे