星期四, 九月 14, 2006

(VB.NET)建立,结束进程

建立一个进程:

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

没有评论: