yet another blog about computer, technology, programming, and internet

Tuesday, June 27, 2006

Creating a Restore Point Using Windows Management Instrumentation (WMI) and C#.NET

Tuesday, June 27, 2006 Posted by Ismail Habib , , 36 comments
I have tried to find a way to create a restore point in Windows, but no matter how hard I tried, I just can't find the one using C#, one of my favourite language. Sigh, I have to make one on my own :P Windows has something called WMI (Windows Management Instrumentation). It contains a huge classes and objects that allow programmers to do a lot of things, creating a restore point is just one of them.
Here is the source code to create a restore point in Windows:

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"] = "My Restore Point";
oInParams["RestorePointType"] = 0;
oInParams["EventType"] = 100;

ManagementBaseObject oOutParams = oProcess.InvokeMethod("CreateRestorePoint", oInParams, null);


Hope this will be useful. Don't forget to add System.Management as a reference!

36 comments:

  1. Tester12:17 AM

    I get an error on Vista using above code.

    Unhandled Exception: System.Runtime.InteropServices.COMException (0x800423F4)
    at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode
    , IntPtr errorInfo)
    at System.Management.ManagementObject.InvokeMethod(String methodName, Managem
    entBaseObject inParameters, InvokeMethodOptions options)

    ReplyDelete
  2. Thanks for the comment. Unfortunately, this code is written for Windows XP and I am not sure if it's going to work in another version of Windows.

    ReplyDelete