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

下面收集了一些例子;

获取本地硬盘的信息:

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

 

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

xDotNet

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值