如何在 C# 中删除 Windows 还原点?

发布于 2024-07-13 18:54:38 字数 75 浏览 3 评论 0原文

我正在寻找一种使用 C# 或许通过调用 WMI 来删除 Windows 还原点的方法。

任何代码片段都会非常有帮助。

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

Any code snippet would be very helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2024-07-20 18:54:38

谈到 Morten 所说的你可以使用该 API。 据我所知,WMI 不提供删除还原点的方法。 如果您有序列号,SRRemoveRestorePoint 可以删除还原点。 您可以通过 WMI 获得它。 这是我删除还原点的代码。

[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);
}

我只是输入了 335,因为这是我在系统上能找到的最远的一个。 计数很可能从 1 开始并不断递增。 所以它并不像在数组中拥有一个索引那么简单。

至于获取序列号,我将代码从 Microsoft 转换为 C#,这将为您提供信息。 请务必添加 System.Management 作为参考。 否则这段代码将无法正常工作。

    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());
    }

我在我的盒子(顺便说一下 Vista)上测试了这个,它工作没有问题。 还必须以管理员身份运行,但我想你已经想到了这一点。

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.

鸩远一方 2024-07-20 18:54:38

虽然我对 WMI 一无所知,但资源可能会帮助您入门。 它不会直接触及您的问题,但也许它在某种程度上有用。 无论如何,相关的Win32/COM函数似乎是SRRemoveRestorePoint。 我希望这有任何用处。

或者,如果您愿意,也可以使用 VBScript

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文