【问题标题】:Creating a Windows Restore Point?创建 Windows 还原点?
【发布时间】:2018-11-24 14:45:57
【问题描述】:

所以我有这段代码,但我遇到了一些迄今为止我无法解决的问题:

任何有参数的地方,即"CreateRestorePoint"inParams 参数,我都会得到绿色波浪下划线,表示使用(例如)"NameOf(CreateRestorePoint) instead of specifying the program element name"

但是,无论我这样做还是离开,我都会收到同样的错误:

System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: path'

代码:

    Public Function CreateRestorePoint(Description As String, EventType As Integer, RestorePointType As Integer) As Integer

        Try

            Dim classInstance As New ManagementObject("root\DEFAULT", "SystemRestore", Nothing)

            ' Obtain [in] parameters for the method
            Dim inParams As ManagementBaseObject = classInstance.GetMethodParameters("CreateRestorePoint")

            ' Add the input parameters
            inParams("Description") = Description
            inParams("EventType") = EventType
            inParams("RestorePointType") = RestorePointType

            ' Execute the method and obtain the return values
            Dim outParams As ManagementBaseObject = classInstance.InvokeMethod("CreateRestorePoint", inParams, Nothing)

            ' List outParams
            Debug.Print("Out parameters: ")
            Debug.Print("ReturnValue: {0}", outParams("ReturnValue"))

            CreateRestorePoint = 1

        Catch err As ManagementException

            Debug.Print(String.Format("An error occurred while trying to execute the WMI method: {0}", err.Message))

        End Try

        Return CreateRestorePoint

    End Function

这是我调用函数的方式:

    Dim CRP As New JSEWindowsRestore.WindowsRestoreFunctions

    CRP.CreateRestorePoint(String.Format("Test Restore Point: {0}", DateTime.Now), 100, 12)

有人发现问题了吗?

【问题讨论】:

  • 错误发生在哪一行?

标签: vb.net wmi


【解决方案1】:

一切看起来都不错。您唯一需要更改的是 ManagementClass 前几行中的 ManagementObject。

Dim classInstance As New ManagementClass("root\DEFAULT", "SystemRestore", Nothing)

ManagementObject 是指类的一个实例,而 ManagementClass 是指类本身。您收到的 path 错误是因为代码需要一个实例路径而不是类本身。

至于绿色的波浪线,它们不应该阻止你编译,但它可能的 Visual Studio 会更喜欢这种语法。

        inParams.Properties("Description").Value = Description
        inParams.Properties("EventType").Value = EventType
        inParams.Properties("RestorePointType").Value = RestorePointType

另外,请确保应用程序具有管理员权限,否则当您尝试调用此方法时,您将被拒绝访问。

【讨论】:

  • 为什么在这里投反对票?这个信息是正确的。可能是部分问题,但这是 OP 代码的直接问题。
  • 可能需要设置一个ConnectionOptions对象,定义访问权限和模拟类型。然后电话将是Dim conOptions As ConnectionOptions = New ConnectionOptions() conOptions.EnablePrivileges = True conOptions.Authentication = Management.AuthenticationLevel.PacketPrivacy Dim classInstance As New ManagementClass("root\DEFAULT", "SystemRestore", New ObjectGetOptions(conOptions.Context))
  • @J. Scott Elblein 这通常意味着您的卷影复制 (VSS) 服务和/或 Microsoft 软件卷影复制提供程序 (swprv) 未运行或无法运行。
  • 它不会工作,我已经知道了,除了从 Win8 开始发布的解决方案之外,还有更多工作要做......我自己有一个解决方案,但弄清楚第二部分一直是个问题......它与创建注册密钥有关。如果密钥不存在,则默认情况下将应用以下内容。如果应用程序调用 CreateRestorePoint 方法来创建还原点,并且在过去 24 小时内创建了任何还原点,Windows 将跳过创建此新还原点。 CreateRestorePoint 方法返回 S_OK。 docs.microsoft.com/en-us/windows/desktop/sr/…
  • 恕我直言,缺少很多信息,正如我提到的,我有一个可以运行的解决方案,但第二部分我还没有下来,我想提供一个完整的解决方案,因为你会遇到无论哪种方式...还要确认卷影副本(VSS)是否已打开,手动创建备份,如果它可以正常工作...
猜你喜欢
  • 2023-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多