zoukankan      html  css  js  c++  java
  • 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
    {
        /// <summary>
        /// wServiceHandler 的摘要说明
        /// </summary>
        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);
                }
            }
    
            /// <summary>
            /// 启动服务
            /// </summary>
            /// <param name="serviceName">服务名</param>
            private void StartService(string serviceName)
            {
                ServiceController service = new ServiceController(serviceName);
                if (service.Status == ServiceControllerStatus.Stopped)
                {
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running);
                    service.Close();
                }
            }
    
            /// <summary>
            /// 停止服务
            /// </summary>
            /// <param name="serviceName">服务名</param>
            private void StopService(string serviceName)
            {
                ServiceController service = new ServiceController(serviceName);
                if (service.Status == ServiceControllerStatus.Running)
                {
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped);
                    service.Close();
                }
            }
    
            /// <summary>
            /// 重启服务
            /// </summary>
            /// <param name="serviceName">服务名</param>
            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" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <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<ServiceModel> list=new List<ServiceModel>();
            protected void Page_Load(object sender, EventArgs e)
            {
                ServiceController[] myServices = ServiceController.GetServices();
    
                list = new List<ServiceModel>();
                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);
                    }
                }
            }
        }
    }
    

    运行结果如图:

    这里写图片描述


    这里写图片描述

  • 相关阅读:
    2-SAT模板
    AC自动机
    省选预备营-Day3(图论) 总结
    省选预备营-Day2(分治) 总结
    左偏树(可并堆)总结
    省选预备营-Day1(数据结构) 总结
    OI基础知识
    C++ 堆
    CH4601 普通平衡树
    java 函数形参传值和传引用的区别
  • 原文地址:https://www.cnblogs.com/Wulex/p/6961846.html
Copyright © 2011-2022 走看看