首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > windows >

C#起动和停止windows服务

2013-10-08 
C#启动和停止windows服务效果图如下:前台js如下:public class ServerHandler : IHttpHandler{public void

C#启动和停止windows服务

效果图如下:

C#起动和停止windows服务

前台js如下:

  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(serverName))            {                context.Response.Write("empty");            }            if (action == "start")            {                byte[] ver = new byte[1024];                try                {                    //开启服务                                                     if (CommonClass.StartWindowsService(serverName))                            {                                context.Response.Write("run");                            }                            else                            {                                context.Response.Write("startfail");                            }                }                catch (Exception ex)                {                    context.Response.Write("提示:"+ex.Message);                }            }            else if (action == "stop") //停止服务            {                try                {                        if (CommonClass.StopWindowsService(serverName))                        {                            //Thread.Sleep(6000*3);                            context.Response.Write("end");                        }                        else                        {                            context.Response.Write("stopfail");                        }                }                catch (Exception ex)                {                    if (ex.Message == "超时时间已到而操作尚未完成。")                    {                        context.Response.Write("提示:" + ex.Message);                    }                    else                    {                        context.Response.Write("NoNormalEnd");                    }                }            }        }        public bool IsReusable        {            get            {                return false;            }        }    }


热点排行