启动和暂停services(C#)

        /// <summary>
        /// ensure the service is on local computer
        /// </summary>
        /// <param name="serviceName">service name</param>
        /// <returns>true or false</returns>
        public bool IsServiceExisted(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
                if (sc.ServiceName == serviceName)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// start the service
        /// </summary>
        /// <param name="serviceName">the service name</param>
        /// <param name="timeOut">the time out of stop service</param>
        /// <returns>true or false</returns>
        public bool StartService(string serviceName, int timeOut)
        {
            bool isSuccess = false;
            if (IsServiceExisted(serviceName))
            {
                ServiceController service = new ServiceController(serviceName);
                if (ServiceControllerStatus.Running != service.Status &&
                    ServiceControllerStatus.StartPending != service.Status)
                {
                    service.Start();
                    service.Refresh();
                    System.Threading.Thread.Sleep(timeOut);
                    service.Refresh();
                    if (ServiceControllerStatus.Running == service.Status)
                    {
                        isSuccess = true;
                    }
                }
                else
                {
                    isSuccess = true;
                }
            }

            return isSuccess;
        }
        /// <summary>
        /// stop the service
        /// </summary>
        /// <param name="serviceName">the service name</param>
        /// <param name="timeOut">the time out of stop service</param>
        /// <returns>success or failed</returns>
        public bool StopService(string serviceName, int timeOut)
        {
            bool isSuccess = false;
            if (IsServiceExisted(serviceName))
            {
                ServiceController service = new ServiceController(serviceName);
                if (ServiceControllerStatus.Running == service.Status)
                {
                    service.Stop();
                    service.Refresh();
                    System.Threading.Thread.Sleep(timeOut);
                    service.Refresh();
                    if (ServiceControllerStatus.Stopped == service.Status)
                    {
                        isSuccess = true;
                    }
                }
                else
                {
                    isSuccess = true;
                }
            }
            return isSuccess;
        }

PS:以Administrator身份才会成功,否则failed,并抛出异常

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# Web Services是一种利用.NET Framework提供的技术,允许应用程序之间进行通信的方式。它们通常基于XML格式,可以是SOAP(Simple Object Access Protocol)服务,也可以是RESTful API,支持HTTP协议。C# Web Services的主要特点是跨平台、可序列化和易于访问。 1. **SOAP Web Services**:使用.NET的WebService类和SoapHttpClientClass创建,提供了一种标准的方式来交换结构化的数据。通过WSDL (Web Service Description Language) 描述了服务的功能、接口和消息格式,使得客户端和服务端可以相互理解和调用。 2. **ASMX Web Services**:早期版本的C# Web Services,基于XMLHttpRequest,使用.asmx文件作为入口点。虽然现在不太常用,但在一些旧系统中仍然可见。 3. **RESTful Web Services**:采用HTTP方法(GET、POST、PUT、DELETE)和URI来表示资源和操作,通常返回JSON或XML数据。这种风格更轻量级,易于理解和使用。 4. **WCF (Windows Communication Foundation)**:是.NET框架的一个重要组成部分,它提供了更高级别的API,可以创建各种类型的Web Services,包括SOAP、RESTful等,支持安全性和并发处理等功能。 C# Web Services的优点有: - 简单易用:可以直接从.NET框架获取大量现成的支持。 - 跨语言兼容:因为XML格式,所以可以跨平台、跨语言地通信。 - 强类型:利用.NET的数据契约特性,保证了数据的安全性和准确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值