如何检查是否在 C# 中安装了 Windows 服务

编程入门 行业动态 更新时间:2024-10-08 00:32:51
本文介绍了如何检查是否在 C# 中安装了 Windows 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我编写了一个 Windows 服务,它向安装在同一台机器上的 GUI 公开 WCF 服务.当我运行 GUI 时,如果我无法连接到服务,我需要知道是因为尚未安装服务应用程序,还是因为服务没有运行.如果是前者,我想安装它(如 这里);如果是后者,我想启动它.

I've written a Windows Service that exposes a WCF service to a GUI installed on the same machine. When I run the GUI, if I can't connect to the service, I need to know if it's because the service app hasn't been installed yet, or if it's because the service is not running. If the former, I'll want to install it (as described here); if the latter, I'll want to start it up.

问题是:如何检测服务是否已安装,然后检测到已安装,如何启动?

Question is: how do you detect if the service is installed, and then having detected that it's installed, how do you start it up?

推荐答案

使用:

// add a reference to System.ServiceProcess.dll
using System.ServiceProcess;

// ...
ServiceController ctl = ServiceController.GetServices()
    .FirstOrDefault(s => s.ServiceName == "myservice");
if(ctl==null)
    Console.WriteLine("Not installed");
else    
    Console.WriteLine(ctl.Status);

这篇关于如何检查是否在 C# 中安装了 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 20:27:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142749.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:装了   中安   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!