Web启动,停止Windows服务

When you grow stronger,the world become more dangerous.当你变得越强大,这个世界反而会变得越危险。

ServiceModel.cs代码:

  public class ServiceModel
    {
        public string ServiceName { get; set; }

        public string DisplayName { get; set; }

        public bool IsRunning { get; set; }
    }

wServiceHandler.ashx代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Web;

namespace wServiceManager
{
    /// 
    /// wServiceHandler 的摘要说明
    /// 
    public class wServiceHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            //服务名
            string serviceName = context.Request["serviceName"];
            //操作类型【重启、停止、重启】
            string type = context.Request["type"];

            try
            {
                switch (type)
                {
                    case "start":
                        StartService(serviceName);
                        break;
                    case "stop":
                        StopService(serviceName);
                        break;
                    case "reset":
                        ResetService(serviceName);
                        break;
                    default:
                        ResetService(serviceName);
                        break;
                }

                context.Response.Write("ok");
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
            }
        }

        /// 
        /// 启动服务
        /// 
        /// 服务名
        private void StartService(string serviceName)
        {
            ServiceController service = new ServiceController(serviceName);
            if (service.Status == ServiceControllerStatus.Stopped)
            {
                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running);
                service.Close();
            }
        }

        /// 
        /// 停止服务
        /// 
        /// 服务名
        private void StopService(string serviceName)
        {
            ServiceController service = new ServiceController(serviceName);
            if (service.Status == ServiceControllerStatus.Running)
            {
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped);
                service.Close();
            }
        }

        /// 
        /// 重启服务
        /// 
        /// 服务名
        private void ResetService(string serviceName)
        {
            ServiceController service = new ServiceController(serviceName);
            if (service.Status == ServiceControllerStatus.Running || service.Status == ServiceControllerStatus.Stopped)
            {
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped);
                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running);
                service.Close();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

IndexManager.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="IndexManager.aspx.cs" Inherits="wServiceManager.IndexManager" %>



    
        
        
        
        
        
    
    
        

Windows服务管理

<% int count = list.Count; for (int i = 0; i < count; i++) { string dname = list[i].DisplayName.Trim(); string sname = list[i].ServiceName.Trim(); string isRun = list[i].IsRunning ? "运行中" : "停止中"; %> <% } %>
服务标识的名称 服务的友好名称 状态 操作
<%= dname %> <%= sname %> <%= isRun %> <% if (list[i].IsRunning) { %> <% } else { %> <% } %>

IndexManager.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceProcess;

namespace wServiceManager
{
    public partial class IndexManager : System.Web.UI.Page
    {
        public List list=new List();
        protected void Page_Load(object sender, EventArgs e)
        {
            ServiceController[] myServices = ServiceController.GetServices();

            list = new List();
            foreach (var item in myServices)
            {
                if (item.ServiceType == ServiceType.Win32OwnProcess && item.DisplayName.Contains("memcached"))
                {
                    ServiceModel model = new ServiceModel();
                    model.ServiceName = item.ServiceName;
                    model.DisplayName = item.DisplayName;
                    if(item.Status == ServiceControllerStatus.Running)
                    {
                        model.IsRunning = true;
                    }
                    else
                    {
                        model.IsRunning = false;
                    }
                    list.Add(model);
                }
            }
        }
    }
}

运行结果如图:

这里写图片描述


这里写图片描述

转载于:https://www.cnblogs.com/Wulex/p/6961846.html

你可能感兴趣的:(Web启动,停止Windows服务)