VB.NET GetObject(" winmgmts,后期绑定

时间:2015-03-19 20:02:20

标签: vb.net wmi

我正在尽力不使用getObject函数的后期绑定。我怎么知道我不会被瞧不起只在一堂课中严格限制。

我的问题是我无法找到宣布我的会员类型的内容。

         Dim restPoint = GetObject("winmgmts:\\.\root\default:Systemrestore")

    If restPoint IsNot Nothing Then

        If restPoint.CreateRestorePoint("test restore point system", 12, 100) = 0 Then
            MsgBox("Restore Point created successfully")
        Else
            MsgBox("Could not create restore point!")
        End If
    End If

我花了几个小时试图研究msdn createrestorepoint来自。我不想直接使用WMI或严格禁用。

由于

1 个答案:

答案 0 :(得分:0)

我想我现在会选择WMI。那个片段清理干净了。

Imports System.Management

Public Class CreateRestorePoint

''' <summary>
''' Defines the search query for the ManagementScope path.
''' </summary>
Private Const MANAGEMENT_SCOPE As String = "\\localhost\root\default:SystemRestore"

''' <summary>
''' Attempts to create a new restore point using WMI.
''' </summary>
''' <returns>True if the restore point was created, other wise false.</returns>
''' <remarks>
''' Gets the object containing the input parameters to a method, and then fills in the values and passes the object to the InvokeMethod call.
''' </remarks>
Friend Function CreateRestorePoint() As Boolean

    Dim created As Boolean = True

    Using wmiQuery As New ManagementClass(New ManagementPath(MANAGEMENT_SCOPE))

        Dim query = GetParams(wmiQuery)

        Try
            wmiQuery.InvokeMethod("CreateRestorePoint", query, Nothing)
        Catch ex As ManagementException
            created = False
        End Try

    End Using

    Return created

End Function

''' <summary>
''' Sets the ManagementBaseObject parameters.
''' </summary>
''' <param name="wmiQuery"></param>
''' <returns>Returns a ManagementBaseObject representing the list of input parameters for a method.</returns>
''' <remarks>The members of this class enable you to access WMI data using a specific WMI class path.></remarks>
Private Function GetParams(wmiQuery As ManagementClass) As ManagementBaseObject

    Dim query = wmiQuery.GetMethodParameters("CreateRestorePoint")

    query("Description") = "-CautionSparta"
    query("RestorePointType") = 12
    query("EventType") = 100

    Return query

End Function

End Class
相关问题