C# Start and Stop Services

C#启动和关闭服务可采用以下步骤:

  1. 添加引用:System.ServiceProcess
  2. using System.ServiceProcess;
  3. 代码如代码片
  4. 管理员权限运行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;

namespace StartAndStopService
{
    class Program
    {
        static void Main(string[] args)
        {
            //此处赋值本机服务名
            string ServiceName = " ";
            ToStartService(ServiceName);
            ToStopService(ServiceName);
 
            Console.ReadKey();
        }


        static void ToStopService(string ServiceName)
        {
            if (ServiceName != null)
            {
                //1.find service
                System.ServiceProcess.ServiceController toStoppedService = new System.ServiceProcess.ServiceController();
                toStoppedService.ServiceName = ServiceName;
                toStoppedService.MachineName = ".";

                //2.check if this service is stopped
                if (toStoppedService.Status == ServiceControllerStatus.Running)
                {
                    Console.WriteLine("Service [{0}] is running and Start to stop it.", ServiceName);
                    if (toStoppedService.CanStop == true)
                    {
                        toStoppedService.Stop();

                    }
                    else
                    {
                        Console.WriteLine("Service [{0}] cannot be stopped.", ServiceName);

                    }

                }
                else
                {
                    Console.WriteLine("Service [{0}] has already been stopped and Skipped stopping this time.", ServiceName);
                }


                //3.refresh service, change status and add trace
                DateTime dt = DateTime.Now.AddSeconds(30);
                while (DateTime.Now.CompareTo(dt) < 0 && toStoppedService.Status != ServiceControllerStatus.Stopped)
                {
                    toStoppedService.Refresh();
                }

                if (toStoppedService.Status == ServiceControllerStatus.Stopped)
                {
                    Console.WriteLine("Service [{0}] is stopped successfully.", ServiceName);
                }
            }
        }

        static void ToStartService(string ServiceName)
        {
            if (ServiceName != null)
            {
                //1.find service
                System.ServiceProcess.ServiceController toStartService = new System.ServiceProcess.ServiceController();
                toStartService.ServiceName = ServiceName;
                toStartService.MachineName = ".";

                //2.check already running service
                if (toStartService.Status == ServiceControllerStatus.Running)
                {
                    Console.WriteLine("Service [{0}] has already been running and Restarting it now.", ServiceName);
                    toStartService.Stop();
                    toStartService.WaitForStatus(ServiceControllerStatus.Stopped);
                    toStartService.Start();
                    toStartService.WaitForStatus(ServiceControllerStatus.Running);
                }
                else
                {
                    toStartService.Start();
                }


                //3.refresh service, change status and add trace
                DateTime dt = DateTime.Now.AddSeconds(30);
                while (DateTime.Now.CompareTo(dt) < 0 && toStartService.Status != ServiceControllerStatus.Running)
                {
                    toStartService.Refresh();
                }

                if (toStartService.Status == ServiceControllerStatus.Running)
                {
                    Console.WriteLine("Service [{0}] starts successfully.", ServiceName);
                }

            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值