sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

使用C#&.NET Core编程实现获取所有Windows服务列表及对Windows服务(Windows Service)的启动/停止/重启的方法
原文链接:https://codedefault.com/p/start-stop-restart-install-uninstall-windows-service-in-csharp-application

前言

在Windows操作系统中,我们可以在命令提示符中运行servcies.msc打开Windows服务的窗口,这个窗口中会罗列出此操作系统中当前所有已安装的Windows服务。我们可以右键单击任意一个服务,然后对其进行相应的启动/停止/重启等操作,如图:

Windows服务列表

本文将向C#开发者们介绍如何使用编程实现获取所有Windows服务列表及对Windows服务(Windows Service)的启动/停止/重启的方法。

获取当前Windows操作系统所有的服务列表

在使用ServiceController之前,请引入命名空间:

登录后复制
using System.ServiceProcess;

获取当前Windows操作系统所有的服务列表的方法ServiceController.GetServices(),如下:

登录后复制
public static List<ServiceController> GetAllServices() { return ServiceController.GetServices().ToList(); }

获取单个指定服务名称的Windows服务为:

登录后复制
public static ServiceController GetService(string serviceName) { ServiceController[] services = ServiceController.GetServices(); return services.FirstOrDefault(_ => _.ServiceName.ToLower() == serviceName.ToLower()); }

判断一个指定的服务是否正在运行:

登录后复制
public static bool IsServiceRunning(string serviceName) { ServiceControllerStatus status; uint counter = 0; do { ServiceController service = GetService(serviceName); if (service == null) { return false; } Thread.Sleep(100); status = service.Status; } while (!(status == ServiceControllerStatus.Stopped || status == ServiceControllerStatus.Running) && (++counter < 30)); return status == ServiceControllerStatus.Running; }

判断一个指定的服务是否已安装:

登录后复制
public static bool IsServiceInstalled(string serviceName) { return GetService(serviceName) != null; }

启动一个指定名称的服务

登录后复制
public static void StartService(string serviceName) { ServiceController controller = GetService(serviceName); if (controller == null) { return; } controller.Start(); controller.WaitForStatus(ServiceControllerStatus.Running); }

停止一个指定名称的服务

登录后复制
public static void StopService(string serviceName) { ServiceController controller = GetService(serviceName); if (controller == null) { return; } controller.Stop(); controller.WaitForStatus(ServiceControllerStatus.Stopped); }

重启一个指定名称的服务

重启一个指定名称的服务需要首先找到服务,如果此服务正在运行,则需要停止,然后再启动服务,具体代码如下:

登录后复制
public static void RestartService(string serviceName) { int timeoutMilliseconds = 50; ServiceController service = new ServiceController(serviceName); try { int millisec1 = 0; TimeSpan timeout; if (service.Status == ServiceControllerStatus.Running) { millisec1 = Environment.TickCount; timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } int millisec2 = Environment.TickCount; timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1)); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch (Exception e) { //Trace.WriteLine(e.Message); } }

.NET Core程序获取Windows服务列表

如果是以.NET Core的程序中,请安装System.ServiceProcess.ServiceControllerNuget程序包,然后再调用ServiceController.GetServices()方法读取所有的Windows服务列表,示例如下:

登录后复制
using System.ServiceProcess; Run(); Console.ReadKey(); static void Run() { var services = ServiceController.GetServices(); Console.WriteLine("Windows Services:"); foreach (var service in services) { Console.WriteLine($"Name:{service.ServiceName}==>{service.Status.ToString()}"); } }

总结

下面提供一个关于C#操作Windows服务的封装类,如下:

登录后复制
using System; using System.Collections.Generic; using System.Linq; using System.ServiceProcess; using System.Threading; namespace ConsoleApp1 { public class ServiceManager { public static List<string> GetAllServiceName() { var serviceNameList = ServiceController.GetServices().Select(x => x.ServiceName); return serviceNameList.ToList(); } public static List<ServiceController> GetAllServices() { return ServiceController.GetServices().ToList(); } public static ServiceController GetService(string serviceName) { ServiceController[] services = ServiceController.GetServices(); return services.FirstOrDefault(_ => _.ServiceName.ToLower() == serviceName.ToLower()); } public static bool IsServiceRunning(string serviceName) { ServiceControllerStatus status; uint counter = 0; do { ServiceController service = GetService(serviceName); if (service == null) { return false; } Thread.Sleep(100); status = service.Status; } while (!(status == ServiceControllerStatus.Stopped || status == ServiceControllerStatus.Running) && (++counter < 30)); return status == ServiceControllerStatus.Running; } public static bool IsServiceInstalled(string serviceName) { return GetService(serviceName) != null; } public static void StartService(string serviceName) { ServiceController controller = GetService(serviceName); if (controller == null) { return; } controller.Start(); controller.WaitForStatus(ServiceControllerStatus.Running); } public static void StopService(string serviceName) { ServiceController controller = GetService(serviceName); if (controller == null) { return; } controller.Stop(); controller.WaitForStatus(ServiceControllerStatus.Stopped); } public static void RestartService(string serviceName) { int timeoutMilliseconds = 50; ServiceController service = new ServiceController(serviceName); try { int millisec1 = 0; TimeSpan timeout; if (service.Status == ServiceControllerStatus.Running) { millisec1 = Environment.TickCount; timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } // count the rest of the timeout int millisec2 = Environment.TickCount; timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1)); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch (Exception e) { //Trace.WriteLine(e.Message); } } } }

版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。

posted on 2023-03-24 11:31  sunny123456  阅读(1065)  评论(0编辑  收藏  举报