vb中利用WMI判断系统服务状态

前端之家收集整理的这篇文章主要介绍了vb中利用WMI判断系统服务状态前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


程序代码

Private Sub Command1_Click()

On Error GoTo err

Dim objSWbemLocator As SWbemLocator

Dim objSWbemServices As SWbemServices

Dim objSWbemObjectSet AsSWbemObjectSet

Dim objSWbemObject As SWbemObject

Dim strComputer As String,strNameSpace AsString,strClass As String

Dim sstr As String

sstr = Text2.Text

'判断服务是否启动

strComputer = "."

strNameSpace = "root/cimv2"

strClass = "Win32_Service"

Set objSWbemLocator =CreateObject("WbemScripting.SWbemLocator")

Set objSWbemServices =objSWbemLocator.ConnectServer(strComputer,strNameSpace)

Set objSWbemObjectSet =objSWbemServices.ExecQuery("Select * FROM " & strClass &" where name='" & sstr& "'")

For Each objSWbemObject InobjSWbemObjectSet

Text1.Text = "服务状态:" + objSWbemObject.State _

+ Chr(13) + Chr(10) + "启动方式:" +objSWbemObject.StartMode

Next

Exit Sub

err:

Text1.Text = "服务名不对!"

End Sub

如果服务已经启动需要关闭把后面这样修改,如防火墙

程序代码程序代码

For Each objSWbemObject InobjSWbemObjectSet

Text1.Text = "系统防火墙状态:" +objSWbemObject.State + " 启动方式:" +objSWbemObject.StartMode + ".."

fwstatus = objSWbemObject.State

Next

If fwstatus = "Running" Then

Text1.Text = "系统防火墙开启,将被自动关闭......"

Shell "net stopsharedaccess"

End If

猜你在找的VB相关文章