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