C#改变windows服务状态的方法有哪些

本篇内容主要讲解“C#改变windows服务状态的方法有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#改变windows服务状态的方法有哪些”吧!

C#windows服务状态、启动和停止服务1:

先引用System.ServiceProcess.dll

C#windows服务状态、启动和停止服务2:

在引用命名空间using System.ServiceProcess;

ServiceController sc = new ServiceController("Server");  //建立服务对象   //服务运行则停止服务    if (sc.Status.Equals(   ServiceControllerStatus.Running))   {       sc.Stop();     sc.Refresh();    }   //服务停止则启动服务    if ((sc.Status.Equals(   ServiceControllerStatus.Stopped) ) || (sc.Status.Equals(  ServiceControllerStatus.StopPending) ))   {        sc.Start();        sc.Refresh();    }

到此,相信大家对“C#改变windows服务状态的方法有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!