日期:2014-05-16  浏览次数:20831 次

C#启动和停止windows服务

效果图如下:

前台js如下:

  <script type="text/javascript">
        function showLoading(desc) {
            $("body").append("<div id=\"processingdiv\" style=\"display:none;\"><div class=\"popup\"> <div class=\"popup-body\"><div class=\"loading\"><span style='width:128px; height:128px;'><img src='../img/progress.gif' /></span><span class='spnContent'>" + desc + "</span></div></div></div></div>");
            //alert($("head").html());  
            $.openPopupLayer({
                name: "processing",
                width: 500,
                target: "processingdiv"
            });
        }
        function hideLoading() {
            $.closePopupLayer('processing');
            $("#processingdiv").remove();
        }  
    function changeShowStatus(){
        $.post("Ajax/ShowHandler.ashx", { "action": "ChangeStatusShow" }, function (data) {
            $("#spnServerStatus").text(data);
            hideLoading();
        });
    }
    var isValidServerStatus = function (data) {
        if (data == "run") {
            $("#serverStatus").text("停止").css("color", "red");
            changeShowStatus();
            //setTimeout(changeShowStatus, 6000);
        }
        else if (data == "end") {
            $("#serverStatus").text("启动").css("color", "green");
            changeShowStatus();
            //setTimeout(changeShowStatus, 6000);
        }
        else if (data == "NoNormalEnd") {
            $("#serverStatus").text("启动").css("color", "green");
            changeShowStatus();
        }
        else if (data == "empty") {
            alert('服务不存在!');
        }
        else if (data == "startfail") {
            alert('启动失败!');
            $("#serverStatus").text("启动").css("color", "green");
            changeShowStatus();
        }
        else if (data == "stopfail") {
            alert("停止失败!");
            $("#serverStatus").text("停止").css("color", "red");
            changeShowStatus();
        }
        else {
            alert('操作失败!' + data);
            window.location.reload();
        }
    }
    $(function () {
        $("#serverStatus").click(function () {
            var txt = $("#serverStatus").text();
            if (txt == "停止") {
                showLoading("服务正在停止......");
                $("#spnServerStatus").text("正在停止...");
                $.post("Ajax/ServerHandler.ashx", { "action": "stop" }, isValidServerStatus);
            }
            else if (txt == "启动") {
                showLoading("服务正在启动......");
                $("#spnServerStatus").text("正在启动...");
                $.post("Ajax/ServerHandler.ashx", { "action": "start" }, isValidServerStatus);
            }
        });
    });
    </script>
一般处理程序如下:

  public class ServerHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            string serverName = QuarrysClass.WindowsServerName;
            EnumServiceStatus status = CommonClass.GetServiceStatus(serverName);
            if (string.IsNullOrEmpty(serve