/// <summary> /// 卸载window服务 /// </summary> public void UninstallService() { if (InstallerConfig == null) { Console.WriteLine("无效的服务配置项!"); return; } ServiceController controller = InstallerUtils.LookupService(InstallerConfig.ServiceName); if (controller != null) { try { TransactedInstaller installer = GetTransactedInstaller(); installer.Uninstall(null); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { Console.WriteLine("服务{0}尚未安装,输入'/?'查看帮助!", InstallerConfig.ServiceName); } }
/// <summary> /// 安装服务器为window服务 /// </summary> public void InstallService() { if (InstallerConfig == null) { Console.WriteLine("无效的服务配置项!"); return; } ServiceController controller = InstallerUtils.LookupService(InstallerConfig.ServiceName); if (controller == null) { try { TransactedInstaller installer = GetTransactedInstaller(); installer.Install(new Hashtable()); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { Console.WriteLine("服务{0}已经存在,请先卸载后再执行此命令!", InstallerConfig.ServiceName); } }
/// <summary> /// 从控制台运行 /// </summary> public bool StartConsole() { if (InstallerConfig == null) { Console.WriteLine("无效的服务配置项!"); return(false); } ServiceController controller = InstallerUtils.LookupService(InstallerConfig.ServiceName); if (controller != null) { if (controller.Status == ServiceControllerStatus.Running) { Console.WriteLine("服务已经启动,不能从控制台启动,请先停止服务后再执行该命令!"); return(false); } } if (WindowsService != null) { WindowsService.StartMode = StartMode.Console; WindowsService.Start(); Console.WriteLine("控制台已经启动......"); return(true); } else { Console.WriteLine("无效的服务启动项!"); return(false); } }
/// <summary> /// 停止服务服务 /// </summary> public void StopService(string serviceName) { if (string.IsNullOrEmpty(serviceName)) { if (InstallerConfig == null) { Console.WriteLine("无效的服务配置项!"); return; } serviceName = InstallerConfig.ServiceName; } if (string.IsNullOrEmpty(serviceName)) { Console.WriteLine("无效的服务名称!"); return; } ServiceController controller = InstallerUtils.LookupService(serviceName); if (controller != null) { if (controller.Status == ServiceControllerStatus.Running) { Console.WriteLine("正在停止服务{0}.....", serviceName); try { controller.Stop(); controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30)); controller.Refresh(); if (controller.Status == ServiceControllerStatus.Stopped) { Console.WriteLine("停止服务{0}成功!", serviceName); } else { Console.WriteLine("停止服务{0}失败!", serviceName); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { Console.WriteLine("服务{0}已经停止!", serviceName); } } else { Console.WriteLine("不存在服务{0}!", serviceName); } }
/// <summary> /// 列出服务 /// </summary> public void ListService(string contains, string status) { if (string.IsNullOrEmpty(contains) && string.IsNullOrEmpty(status)) { contains = "(Paltform Service)"; } //判断第二个参数,看是否为状态 if (!string.IsNullOrEmpty(contains) && contains.Substring(0, 1) == "-") { status = contains; contains = null; } IList <ServiceInformation> list = new List <ServiceInformation>(); if (!string.IsNullOrEmpty(status)) { try { var prefix = status.Substring(0, 1); status = status.Substring(1); if (prefix != "-" || string.IsNullOrEmpty(status)) { Console.WriteLine("输入的参数无效!"); return; } Console.WriteLine("正在读取服务信息......"); ServiceControllerStatus serviceStatus = (ServiceControllerStatus)Enum.Parse(typeof(ServiceControllerStatus), status, true); list = InstallerUtils.GetServiceList(contains, serviceStatus); } catch { Console.WriteLine("输入的状态无效!"); return; } } else { Console.WriteLine("正在读取服务信息......"); list = InstallerUtils.GetServiceList(contains); } if (list.Count == 0) { Console.WriteLine("未能读取到相关的服务信息......"); } else { foreach (var controller in list) { Console.WriteLine("------------------------------------------------------------------------"); Console.WriteLine("服务名:{0} ({1})", controller.ServiceName, controller.Status); Console.WriteLine("显示名:{0}", controller.DisplayName); Console.WriteLine("描 述:{0}", controller.Description); Console.WriteLine("路 径:{0}", controller.ServicePath); } Console.WriteLine("------------------------------------------------------------------------"); } }