Im looking for a way to delete Windows restore points using C# perhaps by invoking WMI.
Any code snippet would be very helpful.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With