using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; namespace ServiceDeamon { public class CheckServiceHelper { private static ServiceController servCtl; private static string ServName = ConfigurationManager.AppSettings["ServiceName"]; public static void CheckStatus() { if (servCtl == null || servCtl.ServiceName != ServName) { servCtl = new ServiceController(ServName); } Console.WriteLine($"服务 {ServName} 的当前状态:{servCtl.Status}"); if (servCtl != null && servCtl.Status == ServiceControllerStatus.Stopped) { Console.WriteLine($"服务已停止,自动启动:{ServName}"); servCtl.Start(); } //Console.WriteLine("CheckStatus"); } } }