创建 Windows 还原点?
Creating a Windows Restore Point?
所以我有了这段代码,并且 运行 遇到了一些我迄今为止无法解决的问题:
任何有参数的地方,即 "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)
有人发现问题吗?
一切看起来都很好。您唯一需要更改的是 ManagementClass 的前几行中的 ManagementObject。
Dim classInstance As New ManagementClass("root\DEFAULT", "SystemRestore", Nothing)
ManagementObject 指的是 class 的实例,而 ManagementClass 指的是 class 本身。您收到的 path
错误是因为代码需要一个实例路径而不是 class 本身。
至于绿色波浪线,它们不应该阻止您编译,但它可能 Visual Studio 会更喜欢这种语法。
inParams.Properties("Description").Value = Description
inParams.Properties("EventType").Value = EventType
inParams.Properties("RestorePointType").Value = RestorePointType
此外,请确保该应用程序具有管理员权限,否则当您尝试调用此方法时,访问将被拒绝。
所以我有了这段代码,并且 运行 遇到了一些我迄今为止无法解决的问题:
任何有参数的地方,即 "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)
有人发现问题吗?
一切看起来都很好。您唯一需要更改的是 ManagementClass 的前几行中的 ManagementObject。
Dim classInstance As New ManagementClass("root\DEFAULT", "SystemRestore", Nothing)
ManagementObject 指的是 class 的实例,而 ManagementClass 指的是 class 本身。您收到的 path
错误是因为代码需要一个实例路径而不是 class 本身。
至于绿色波浪线,它们不应该阻止您编译,但它可能 Visual Studio 会更喜欢这种语法。
inParams.Properties("Description").Value = Description
inParams.Properties("EventType").Value = EventType
inParams.Properties("RestorePointType").Value = RestorePointType
此外,请确保该应用程序具有管理员权限,否则当您尝试调用此方法时,访问将被拒绝。