vb.net 检查windows服务是否存在vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23922375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
check if windows service exists vb.net
提问by DaE
I have the following code to check if a Windows service exists but it keeps throwing an exception if the service does not exist.
我有以下代码来检查 Windows 服务是否存在,但如果该服务不存在,它会不断抛出异常。
Dim controller As New ServiceController("test")
If controller.Status = Nothing Then
Label2.Text = ""
Else
ListBox1.Items.Add("Service found")
End If
What I would like is do nothing if not found and add to listbox if found.
我想要的是如果没有找到什么都不做,如果找到则添加到列表框。
回答by nkvu
That may be by design since ServiceController is meant to "connect to and control the behavior of existing services" (c.f. here). Reading herethe Statusproperty is supposed to throw an InvalidOperationExceptionif the service does not exist.
这可能是设计使然,因为 ServiceController 旨在“连接到现有服务并控制其行为”(参见此处)。如果服务不存在,在这里阅读该Status属性应该抛出一个InvalidOperationException。
What you could do is use the GetServices() methodto list the services on the machine and see if the thing you are looking for exists - perhaps something like:
你可以做的是使用 GetServices()方法列出机器上的服务,看看你要找的东西是否存在——也许是这样的:
Dim servicesButNotDevices As ServiceController() = ServiceController.GetServices()
For Each service As ServiceController In servicesButNotDevices
If service.ServiceName = "my service name" Then 'May also use DispalyName property depending on your use case
'Put in list box
Exit For
End If
Next
If you didn't want to catch and handle the exception
如果您不想捕获和处理异常
回答by nobody
According to the ServiceControllerdocumentation, this is the expected behavior. If the service exists, the constructor makes a ServiceControllerinstance that provides an interface to control the service. If the service does not exist, there is nothing to do so the constructor throws an ArgumentException.
根据该ServiceController文件,这是预期的行为。如果服务存在,构造函数会创建一个ServiceController实例,提供一个接口来控制服务。如果该服务不存在,则无事可做,因此构造函数会抛出一个ArgumentException.
The following code should do what you need:
以下代码应该可以满足您的需求:
Try
Dim controller As New ServiceController("test")
ListBox1.Items.Add("Service found")
Catch ex As ArgumentException
Label2.Text = ""
End Try
回答by DaE
Thanks for everyones input on this.
感谢大家对此的投入。
I used the idea from NKVU but tweaked it a little:
我使用了 NKVU 的想法,但对其进行了一些调整:
For Each service As ServiceController In ServiceController.GetServices()
Dim serviceName As String = service.ServiceName
If serviceName = "mmg" Then
ListBox1.Items.Add(serviceName)
End If
Next
回答by BilgiSoft
Public Shared Function GetService() As ServiceProcess.ServiceController
Try
Dim Services = (From f In ServiceProcess.ServiceController.GetServices Select f Where f.ServiceName = "BilgiSoft").ToList
If Services.Count = 1 Then
Return Services(0)
Exit Function
Else
Return Nothing
Exit Function
End If
Catch ex As Exception
Return Nothing
Exit Function
End Try
End Function

