检查未注册/丢失的服务
如何使用 ServiceController 来告诉我服务是否已注册?在下面的代码片段中,对 null DisplayName 的检查会导致 System.InvalidOperationException。
有没有一种我完全想念的简单方法?
ServiceController sc = new ServiceController("TestService");
if (sc.DisplayName == null)
{
// unregistered or missing service
}
How can I use ServiceController to tell me if a service has been registered or not? In the code fragment below, the check for a null DisplayName results in a System.InvalidOperationException.
Is there a straightforward way of doing this that I'm completely missing?
ServiceController sc = new ServiceController("TestService");
if (sc.DisplayName == null)
{
// unregistered or missing service
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看下面的解决方案...
您似乎无法...来自文档:
这听起来就像服务必须已经注册才能让 ServiceController 与它们一起工作。
我想您可以简单地捕获异常(表明该服务不存在),但这似乎并不是真正的异常,不是吗?
解决方案:
使用
阵列并搜索您所需的服务。如果您只是在本地机器上工作,则可以省略
machineName
参数。http://msdn.microsoft.com/en-us/library/s21fd6th。 ASPX
Look at solution below...
It doesn't appear that you can...From the docs:
Which reads like services must already be registered in order for the ServiceController to work with them.
I guess you could simply catch the exception (indicating the service doesn't exist), but that doesn't seem to be truly exceptional does it?
Solution:
Use
and search the array for your needed services. If you're just working on your local box you can omit the
machineName
argument.http://msdn.microsoft.com/en-us/library/s21fd6th.aspx