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

运行结果如图:


Web启动,停止Windows服务的更多相关文章

  1. delphi 启动停止windows服务 转

    http://blog.csdn.net/haiou327/article/details/6106233 不用cmd用delphi如何实现启动停止windows服务建议参考一下Delphi的Sckt ...

  2. Delphi启动/停止Windows服务,启动类型修改为"自动"

    unit U_StartServices; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Contr ...

  3. Linux Systemd——在RHEL/CentOS 7中启动/停止/重启服务

    RHEL/CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理.systemd兼容SysV和Li ...

  4. bat启动/停止oracle服务

    原文:bat启动/停止oracle服务 自己的电脑比较慢,尤其装了oracle10g后,服务开启和关闭用bat文件操作省事点 开启服务 @echo offnet start OracleService ...

  5. 在CentOS 7中启动/停止/重启服务

    RHEL/CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理.systemd兼容SysV和Li ...

  6. 注册、启动、停止windows服务

    找到本机InstallUtil.exe命令 命令行下注册服务InstallUtil.exe D:\XXXXService.exe 启动服务 net start XXXXService 停止服务net ...

  7. Windows下使用service.bat安装tomcat服务, 启动停止tomcat服务

    在项目开发过程中,以前只是在Eclipse中配置.启动.停止tomcat服务器 如果只想在机器中使用tomcat服务器,而不想安装MyEclipse,可以使用service.bat 将tomcat安装 ...

  8. web站点和windows服务项目发布时如何排除指定文件

    在发布asp.net站点和windows服务项目时,有的时候这样的需求:msbuild编译之后发布到服务器指定目录时要排除指定文件,比如通过jenkins构建时,不希望覆盖原来的Web.config和 ...

  9. 【OF框架】使用OF.WinService项目,添加定时服务,进行创建启动停止删除服务操作

    准备 使用框架搭建完成项目,包含OF.WinService项目. 了解Window Service 和定时服务相关知识. 一.添加一个定时服务 第一步:了解项目结构 第二步:创建一个新的Job 第三步 ...

随机推荐

  1. VTK:VTK嵌入MFC成功

    VTK作为医学显示库,得到较多使用.作为较为上层的设计,对OpenGL进行了封装,并且有Windows.Linux.安卓等开发版本,可移植性较强. 不过VES暂时没有编译成功. 以下是嵌入MFC-ID ...

  2. 通过Static 字段来维护状态是不是一个好主意

    static是申明静态字段.静态方法或者静态类的修饰符.使用static申明的字段属于类型本身而不属于任何字段,声明的类也具有一些特别特性,比如不能实例化,不能继承等.用通俗化的语言来说,static ...

  3. Windows Phone 应用程序的生命周期(二)

    一.App.xaml.cs /// <summary> /// Application 对象的构造函数. /// </summary> public App() { // 未捕 ...

  4. POJ_2594_最小路径覆盖

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8085   Accepted: 3 ...

  5. HDU 2266 How Many Equations Can You Find(模拟,深搜)

    题目 这是传说中的深搜吗....不确定,,,,貌似更加像是模拟,,,, //我要做深搜题目拉 //实际上还是模拟 #include<iostream> #include<string ...

  6. CodeForces 245C-Game with Coins

    题意:给你一个n,紧接着n个正数,然后有一种操作:选择一个x满足(x*2+1<=n)一次可以把下标为  x,2*x,2*x+1的三个数同时减一: 问,最少几次操作可以使n个数字变为零(已经是0的 ...

  7. [bzoj1860 ZJOI2006] 超级麻将 (线性dp)

    传送门 Description Input 第一行一个整数N(N<=100),表示玩了N次超级麻将. 接下来N行,每行100个数a1..a100,描述每次玩牌手中各种牌的数量.ai表示数字为i的 ...

  8. Jquery语法基础

    Jquery语法基础 一.Jquery一般语法格式为:$(selector).action() l  美元符号定义 jQuery (又称工厂函数) l  选择器(selector)“查询”和“查找” ...

  9. mysql绑定多个ip地址

    http://jpuyy.com/2013/07/mysql-bind-multi-address.html mysql绑定多个ip地址 发表于2013 年 7 月 1 日 my.cnf中有选项bin ...

  10. Swift学习——变量var和let常量的用法(一)

    Swift中的变量var和let常量 首先介绍一下Swift中的 var 和 let (1)var 是 variable的缩写形式,是变量的意思 ,是可改变的.并非数据类型 比如: 注意每一个语句后面 ...