VB.Net调用WMI可以实现系统管理和系统信息查询等。WMI全称为Windows Management Instrumentation,利用WMI我们可以实现很多系统管理的功能,msdn在.Net Framework类库中对此描述比较简单(因为早期WMI相关专题有所较详尽描述,故类库中未再详细解说与演示) 乐博网将发布一系列相关文章及实例分析帮助大家更深刻了解它。
开发环境: VB2005 Windows2003
本实例需要在菜单栏中的“项目”-“ 添加引用” -“ System.Management”
并在实例代码中导入引用的项目和程序集中定义的命名空间或编程元素 即: Imports System.Management
创建系统服务的源代码如下:
Imports System Imports System.Management Imports System.Windows.Forms
Namespace WMISample
Public Class CallWMIMethod
Public Overloads Shared Function Main() As Integer
Try
Dim classInstance As New ManagementClass( _ "root\CIMV2", _ "Win32_Service", Nothing)
' Obtain [in] parameters for the method Dim inParams As ManagementBaseObject = _ classInstance.GetMethodParameters("Create")
' Add the input parameters. 乐博网提供
' Execute the method and obtain the return values. Dim outParams As ManagementBaseObject = _ classInstance.InvokeMethod("Create", inParams, Nothing)
' List outParams Console.WriteLine("Out parameters:") Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))
Catch err As ManagementException
MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message) End Try End Function End Class End Namespace |