C#创建还原点网络框架3.5

时间:2018-10-16 12:26:42

标签: c# restore-points

是否可以从Netframework 3.5目标中的C#创建还原点以及所有必需的任务(启用系统保护,调整影子存储大小)? 我找到了一些示例,但是它们都使用System.Management.Automation执行powshershell脚本,而netframework 3.5中没有。 我想做的是将整个动作(如果禁用则启用系统还原,调整影子存储大小并创建还原点)绑定在gui上的按钮上。

1 个答案:

答案 0 :(得分:2)

以下是使用 WMI 的winforms代码,请尽情享受:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class CallWMIMethod
    {
        public new static int Main()
        {
            try
            {
                ManagementObject classInstance = new ManagementObject(@"root\DEFAULT", "SystemRestore.ReplaceKeyPropery='ReplaceKeyPropertyValue'", null);

                // Obtain [in] parameters for the method
                ManagementBaseObject inParams = classInstance.GetMethodParameters("CreateRestorePoint");

                // Add the input parameters.

                // Execute the method and obtain the return values.
                ManagementBaseObject outParams = classInstance.InvokeMethod("CreateRestorePoint", inParams, null);

                // List outParams
                Console.WriteLine("Out parameters:");
                Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"));
            }
            catch (ManagementException err)
            {
                MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
            }
        }
    }
}