C# 注册Windows服务
环境:.NET Farmework 4.7.2
IDE:Visual Studio 2022
添加程序集引用:
System.ServiceProcess
System.Configuration.Install
右键项目,添加UpdateServices1类,让UpdateServices1继承ServiceBase(UpdateServices1换成自己的Services服务)
编译过后如果需要更新服务代码,要在Windows服务里面先停止
const string ServiceName = "ClientUpdateServices"; const string ServiceDescription = "服务描述"; static void Main(string[] args) { if (args.Length > 0) { try { ServiceBase[] serviceToRun = new ServiceBase[] { new UpdateServices1() }; ServiceBase.Run(serviceToRun); } catch (Exception ex) { } } else { StartLable: Console.WriteLine("请选择你要执行的操作——1:自动部署服务(Q),2:安装服务(W),3:卸载服务(E),4:验证服务状态(R),5:退出(T)"); Console.WriteLine("————————————————————"); ConsoleKey key = Console.ReadKey().Key; string conkey = Console.ReadLine(); ServiceHelper.SetServicesDesc(ServiceDescription); if (key == ConsoleKey.NumPad1 || key == ConsoleKey.D1 || key == ConsoleKey.Q || conkey == "1") { if (ServiceHelper.IsServiceExisted(ServiceName)) { ServiceHelper.ConfigService(ServiceName, false); } if (!ServiceHelper.IsServiceExisted(ServiceName)) { ServiceHelper.ConfigService(ServiceName, true); } ServiceHelper.StartService(ServiceName); goto StartLable; } else if (key == ConsoleKey.NumPad2 || key == ConsoleKey.D2 || key == ConsoleKey.W || conkey == "2") { if (!ServiceHelper.IsServiceExisted(ServiceName)) { ServiceHelper.ConfigService(ServiceName, true); } else { Console.WriteLine("\n服务已存在......"); } goto StartLable; } else if (key == ConsoleKey.NumPad3 || key == ConsoleKey.D3 || key == ConsoleKey.E || conkey == "3") { if (ServiceHelper.IsServiceExisted(ServiceName)) { ServiceHelper.ConfigService(ServiceName, false); } else { Console.WriteLine("\n服务不存在......"); } goto StartLable; } else if (key == ConsoleKey.NumPad4 || key == ConsoleKey.D4 || key == ConsoleKey.R || conkey == "4") { if (!ServiceHelper.IsServiceExisted(ServiceName)) { Console.WriteLine("\n服务不存在......"); } else { Console.WriteLine("\n服务状态:" + ServiceHelper.GetServiceStatus(ServiceName).ToString()); } goto StartLable; } else if (key == ConsoleKey.NumPad5 || key == ConsoleKey.D5 || key == ConsoleKey.T || conkey == "5") { } else { Console.WriteLine("\n请输入一个有效键!"); Console.WriteLine("————————————————————"); goto StartLable; } } }
public static class ServiceHelper { static string ServiceDescription = ""; public static void SetServicesDesc(string serviceDesc) { ServiceDescription = serviceDesc; } /// <summary> /// 服务是否存在 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> public static bool IsServiceExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName == serviceName) { return true; } } return false; } /// <summary> /// 启动服务 /// </summary> /// <param name="serviceName"></param> public static void StartService(string serviceName) { if (IsServiceExisted(serviceName)) { System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending) { service.Start(); for (int i = 0; i < 60; i++) { service.Refresh(); System.Threading.Thread.Sleep(1000); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running) { break; } if (i == 59) { throw new Exception("Start Service Error:" + serviceName); } } } } } /// <summary> /// 获取服务状态 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> public static ServiceControllerStatus GetServiceStatus(string serviceName) { System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); return service.Status; } /// <summary> /// 配置服务 /// </summary> /// <param name="serviceName"></param> /// <param name="install"></param> public static void ConfigService(string serviceName, bool install) { TransactedInstaller ti = new TransactedInstaller(); ti.Installers.Add(new ServiceProcessInstaller { Account = ServiceAccount.LocalSystem }); ti.Installers.Add(new ServiceInstaller { DisplayName = serviceName, ServiceName = serviceName, Description = ServiceDescription, StartType = ServiceStartMode.Automatic//运行方式 }); ti.Context = new InstallContext(); ti.Context.Parameters["assemblypath"] = "\"" + Assembly.GetEntryAssembly().Location + "\" /service"; if (install) { ti.Install(new Hashtable()); } else { ti.Uninstall(null); } } }