检查未注册/丢失的服务

发布于 2024-08-07 02:54:54 字数 296 浏览 2 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇年纸鸢 2024-08-14 02:54:54

查看下面的解决方案...您似乎无法...

来自文档

您可以使用ServiceController
类来连接和控制
现有服务的行为。

这听起来就像服务必须已经注册才能让 ServiceController 与它们一起工作。

我想您可以简单地捕获异常(表明该服务不存在),但这似乎并不是真正的异常,不是吗?

解决方案:

使用

var services = ServiceController.GetServices(machineName)

阵列并搜索您所需的服务。如果您只是在本地机器上工作,则可以省略 machineName 参数。

http://msdn.microsoft.com/en-us/library/s21fd6th。 ASPX

Look at solution below... It doesn't appear that you can...

From the docs:

You can use the ServiceController
class to connect to and control the
behavior of existing services.

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

var services = ServiceController.GetServices(machineName)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文