Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete Windows restore points in c#?

Tags:

c#

windows

wmi

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

Any code snippet would be very helpful.

like image 881
Dean Bates Avatar asked Dec 22 '22 13:12

Dean Bates


1 Answers

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.

like image 194
Rob Haupt Avatar answered Jan 05 '23 01:01

Rob Haupt