所以我有这段代码,并且遇到了一些我到目前为止无法解决的问题:
在任何有参数(即"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)
有人发现问题了吗?
答案 0 :(得分:2)
一切看起来都不错。唯一需要更改的是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
此外,请确保该应用程序具有管理员权限,否则当您尝试调用此方法时您将获得拒绝访问的权限。