1

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

4

1 回答 1

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);
            }
        }
    }
}
于 2018-10-16T12:41:09.527 回答