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" %>


<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>title>
        <link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
        <link href="css/bootstrap-theme.css" rel="stylesheet" type="text/css" />
        <script src="js/jquery1.12.4.js" type="text/javascript">script>
        <script src="js/bootstrap.js" type="text/javascript">script>
    head>
    <body>
        <form id="form1" runat="server">
            <div class="container-fluid">
                <div class="row-fluid">
                    <h3>
                        Windows服务管理
                    h3>
                    <table class="table">
                        <thead>
                            <tr>
                                <th>
                                    服务标识的名称
                                th>
                                <th>
                                    服务的友好名称
                                th>
                                <th>
                                    状态
                                th>
                                <th>
                                    操作
                                th>
                            tr>
                        thead>
                        <tbody>
                            <%
                                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 ? "运行中" : "停止中";
                            %>
                                <tr>
                                    <td>
                                        <%= dname %>
                                    td>
                                    <td id="sname">
                                        <%= sname %>
                                    td>
                                    <td>
                                        <%= isRun %>
                                    td>
                                    <td>
                                        <% if (list[i].IsRunning)
                                           { %>
                                            <button class="btn btn-danger" id="stopService" type="button">
                                                停止button>
                                        <%
                                           }
                                           else
                                           { %>
                                            <button class="btn btn-success" id="startService" type="button">
                                                启动button>
                                        <% } %>
                                    td>
                                tr>
                            <% } %>
                        tbody>
                    table>
                div>
            div>
        form>
        <script type="text/javascript">
            var sname = $("#sname").text().trim();
            $("#startService").click(function() {
                $.ajax({
                    type: "post",
                    url: "wServiceHandler.ashx",
                    data: { "serviceName": sname, "type": "start" },
                    success: function(result) {
                        if (result == "ok") {
                            window.location.reload();
                        }

                    }
                });
            });
            $("#stopService").click(function() {
                $.ajax({
                    type: "post",
                    url: "wServiceHandler.ashx",
                    data: { "serviceName": sname, "type": "stop" },
                    success: function(result) {
                        if (result == "ok") {
                            window.location.reload();
                        }
                    }
                });
            });
        script>
    body>
html>

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);
                }
            }
        }
    }
}

运行结果如图:

这里写图片描述


这里写图片描述

你可能感兴趣的:(ASP.NET,Webform)