下面收集了一些例子;
获取本地硬盘的信息:
Imports System
Imports System.Management
Module Module1
Public Sub Main()
Dim disk As ManagementObject = New ManagementObject("win32_logicaldisk.deviceid=""c:""")
disk.Get()
Console.WriteLine("Logical disk size is " + disk("Size").ToString + " bytes")
End Sub
End Module
驱动器类型disk(“DriveType”):
Value=2
Value=3
Value=4
Value=5
Value=6
Floppy disk
Hard disk
network drive
CDROM
RAM Disk
---------------------------------------------------------------------------------------
从新启动远程计算机:(需要权限)
Imports System
Imports System.Management
Module Module1
Public Sub Main()
Dim co As ConnectionOptions = New ConnectionOptions
co.Username = "Admin"'登录的用户名称
co.Password = "12345678"'Password
Dim ms As System.Management.ManagementScope = New System.Management.ManagementScope("\\192.168.1.2\root\cimv2", co)
Dim oq As System.Management.ObjectQuery = New System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem")
Dim mos As ManagementObjectSearcher = New ManagementObjectSearcher(ms, oq)
Dim _query As ManagementObjectCollection = mos.Get()
Dim mo As ManagementObject
For Each mo In _query
Dim tmp() As String = {""}
mo.InvokeMethod("Reboot", tmp)
Console.WriteLine(mo.ToString())
Next
End Sub
End Module
---------------------------------------------------------------------------------------
建立一个进程:
Imports System
Imports System.Management
Module Module1
Public Sub Main()
Dim processClass As ManagementClass = New ManagementClass("Win32_Process")
Dim inParams As ManagementBaseObject = processClass.GetMethodParameters("Create")
inParams("CommandLine") = "calc.exe"
Dim outParams As ManagementBaseObject = processClass.InvokeMethod("Create", inParams, Nothing)
Console.WriteLine("Creation of calculator process returned: " + outParams("returnvalue"))
Console.WriteLine("进程 ID:" & outParams("processId"))
End Sub
End Module
结束一个进程:
Imports System
Imports System.Management
Module Module1
Public Sub Main()
Dim service As ManagementObject = New ManagementObject("win32_service=""winmgmt""")
Dim options As InvokeMethodOptions = New InvokeMethodOptions
options.Timeout = New TimeSpan(0, 0, 0, 5)
Dim outParams As ManagementBaseObject = service.InvokeMethod("StopService", Nothing, options)
Console.WriteLine("状态:" & outParams("Returnvalue"))
End Sub
End Module