tags:

views:

552

answers:

4

Hello,

Im looking for a way to delete Windows restore points using C# perhaps by invoking WMI.

Any code snippet would be very helpful.

Kind regards,

A: 

While I know nothing about WMI, this resource might get you started. It does not directly touch your issue, but perhaps it can be useful somehow. Anyhow, it seems that the relevant Win32/COM function is SRRemoveRestorePoint. I hope this was of any use.

Alternatively, you can work with VBScript, if you're so inclined.

Morten Christiansen
+1  A: 

I can't imagine any reason why you would want to do this. What kind of software are you writing?

tsilb
+1 Don't mess with the operating system's ability to restore itself. I see no use for this at all, since Windows has this functionality already.
Simon Svensson
+2  A: 

Touching on what Morten said you can use that API. WMI doesn't provide a method to delete a Restore Point as far as I can tell. The SRRemoveRestorePoint can remove a restore point, provided you have the sequence number. You can get that through WMI. Here is my code to Remove a restore point.

[DllImport("Srclient.dll")]
public static extern int SRRemoveRestorePoint(int index);

private void button1_Click(object sender, EventArgs e)
{
    int SeqNum = 335;
    int intReturn = SRRemoveRestorePoint(SeqNum);
}

I just threw in 335 since that was the farthest one back as I could find on my system. Its likely that the count starts at 1 and keeps incrementing. so it isn't as simple as just having an index like you would in an array.

As for getting the sequence numbers, I converted the code from Microsoft to C# which will give you that info. Be sure to add System.Management as a reference. Otherwise this code won't work right.

    private void EnumRestorePoints()
    {
        System.Management.ManagementClass objClass = new System.Management.ManagementClass("\\\\.\\root\\default", "systemrestore", new System.Management.ObjectGetOptions());
        System.Management.ManagementObjectCollection objCol = objClass.GetInstances();

        StringBuilder Results = new StringBuilder();
        foreach (System.Management.ManagementObject objItem in objCol)
        {
            Results.AppendLine((string)objItem["description"] + Convert.ToChar(9) + ((uint)objItem["sequencenumber"]).ToString());
        }

        MessageBox.Show(Results.ToString());
    }

I tested this on my box (Vista by the way) and it worked without issue. Also have to be running as Admin, but I think you figured that.

Rob Haupt
A: 

How could i get the sequence number of the restore point?? Thanks a lot!

Don't post questions as answers to other questions -- Use the **Ask Question** button in the upper right to add questions.
Helen