English 中文(简体)
如何在程序上建立系统来恢复点?
原标题:How to create a system restore point programmatically?

我正在寻找一种方法, 以按下按钮的方式创建一个系统, 以当前日期和时间恢复点 。 我试着搜索网络, 寻找一个简单的方法来做到这一点, 但我还没有找到一个 。

我发现这个代码片段来自:http://msdn.microsoft.com/ en-us/library/windows/desktop/a378847%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
问题回答

在这里, sVB.NET 用于创建恢复点的片段( 发现 < a href=" http://www.codeproject.com/Tips/ 49359/ programatically- Create- System-Restore-Point" rel=“ nofollow noreferrer" >这里 ):

Dim restPoint = GetObject("winmgmts:\.
ootdefault: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); 
}

但我改进了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);
var restPoint = GetObject(@"winmgmts:\.
ootdefault:Systemrestore");
if(restPoint!=null)
{
    if(restPoint.CreateRestorePoint("", 0, 100) == 0)
    {
        //do something
    }
    else
    {
         //do something
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签