CSharp操作服务

2011-1-20 16:47:19 - tanlixiao

CSharp操作服务的方法,包括启动服务对象,停止服务,为了避免异常,执行前先判断状态

        /// <summary>
        /// 启动服务
        /// </summary>
        public static void StartServerStatus()
        {
            ServiceController sc = new ServiceController();
            sc.ServiceName = "ServerName";
            if (sc.Status != ServiceControllerStatus.Running)
            {
                sc.Start();
                sc.WaitForStatus(ServiceControllerStatus.Running);
            }
        }
        /// <summary>
        /// 停止服务
        /// </summary>
        public static void StopServerStop()
        {
            ServiceController sc = new ServiceController();
            sc.ServiceName = "ServerName";
            if (sc.Status != ServiceControllerStatus.Stopped)
            {
                sc.Stop();
                sc.WaitForStatus(ServiceControllerStatus.Stopped);
            }
        }

标签:CSharp

关于我

喜欢简简单单的生活.

记录平凡世界的点滴

-于2010年12月31日

联系我

  • 微信:fishren
  • 邮箱:
    tanlixiao@163.com
  • Address:
    中国最南端的滨海城市
    河西区 解放路 502