几个 WMI 的例子(初级) - 1

发表于:2007-07-01来源:作者:点击数: 标签:
WMI - Windows Management Instrumentation,是基于 Web Based Enterprise Management(WBEM)的 面向对象 数据库 ,一个管理企业环境 开发 的标准接口。可以利用它访问本地主机的一些信息和服务、监视计算机、管理远程计算机… 在VS.NET中需要添加引用:Sys

 

 

WMI - Windows Management Instrumentation,是基于 Web Based Enterprise Management(WBEM)的面向对象数据库,一个管理企业环境开发的标准接口。可以利用它访问本地主机的一些信息和服务、监视计算机、管理远程计算机…

在VS.NET中需要添加引用:System.Management.dll,这样你的项目才能使用WMI。

WMI 的使用灵活,但 class 也多,如:CPU的系列号-Win32_Processor、主板BIOS的系列号-Win32_BIOS、本地磁盘-Win32_LogicalDisk、共享资源-Win32_share等等,详细的 WMI 类名可以在MSDN中查询

也可以使用WMI Tools管理WMI服务的名称空间

用这个程序也可以获取WMI Class Name:

 

---------------------------------------------------------------------------------------

    Dim WMI As Management.ManagementClass

    Dim info As Management.ManagementObject

‘ 返回 WMI 的 Class Name 到 String Array

    Private Function FindWMI(ByVal IndexString As String, ByVal Deep As Boolean) As String()

        Try

            Dim _WMIList() As String

            WMI = New Management.ManagementClass

            Dim options As New Management.EnumerationOptions

            Dim chClass As String = Nothing

            If Not IndexString Is Nothing Then

                chClass = IndexString

            End If

            options.EnumerateDeep = Deep

            For Each info In WMI.GetSubclasses(options)

                If _WMIList Is Nothing Then

                    ReDim _WMIList(0)

                Else

                    ReDim Preserve _WMIList(UBound(_WMIList) + 1)

                End If

                If chClass Is Nothing Then

                    _WMIList(UBound(_WMIList)) = (info.Item("__Class"))

                Else

                    If info.Item("__Class").ToString.Substring(0, _

                       IIf(info.Item("__Class").ToString.Length > chClass.Length, _

                       chClass.Length, info.Item("__Class").ToString.Length)).ToLower = chClass Then

                        _WMIList(UBound(_WMIList)) = (info.Item("__Class"))

                    End If

                End If

                Application.DoEvents()

            Next

            options = Nothing

            info = Nothing

            WMI = Nothing

            Return _WMIList

        Catch ex As Exception

            MsgBox(ex.Message, 16 + 0, ex.TargetSite.Name)

        End Try

    End Function

‘获取 WMI 的 Class Info 并将其写入输出窗口

    Private Sub GetInfo(ByVal IndexString As String)

        Try

            If IndexString Is Nothing Then Exit Sub

            Dim wmiClass As String = IndexString

            WMI = New Management.ManagementClass(wmiClass)

            Dim strInfo As String

            Console.WriteLine("WMI Class is:" & wmiClass)

            For Each info In WMI.GetInstances

‘将 WMI Class Info 写到输出窗口

                Console.WriteLine(info.GetText(Management.TextFormat.Mof).ToString)

            Next

            info = Nothing

            WMI = Nothing

        Catch ex As Exception

            MsgBox(ex.Message, 16 + 0, ex.TargetSite.Name)

        End Try

    End Sub

---------------------------------------------------------------------------------------

使用时:

            Dim tmpStr() As String, i As Integer

            ´获取 WMI 的 Class Name

            tmpStr = FindWMI(Nothing, True)

            ´列出 Class Name Array 中的第 559 个 Class 的 Info

            GetInfo(tmpStr(559))

            ´列出物理内存的信息

            GetInfo("Win32_PhysicalMemory")


原文转自:http://www.ltesting.net