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);
}
}
}
}
}
运行结果如图:
Web启动,停止Windows服务的更多相关文章
- delphi 启动停止windows服务 转
http://blog.csdn.net/haiou327/article/details/6106233 不用cmd用delphi如何实现启动停止windows服务建议参考一下Delphi的Sckt ...
- Delphi启动/停止Windows服务,启动类型修改为"自动"
unit U_StartServices; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Contr ...
- Linux Systemd——在RHEL/CentOS 7中启动/停止/重启服务
RHEL/CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理.systemd兼容SysV和Li ...
- bat启动/停止oracle服务
原文:bat启动/停止oracle服务 自己的电脑比较慢,尤其装了oracle10g后,服务开启和关闭用bat文件操作省事点 开启服务 @echo offnet start OracleService ...
- 在CentOS 7中启动/停止/重启服务
RHEL/CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理.systemd兼容SysV和Li ...
- 注册、启动、停止windows服务
找到本机InstallUtil.exe命令 命令行下注册服务InstallUtil.exe D:\XXXXService.exe 启动服务 net start XXXXService 停止服务net ...
- Windows下使用service.bat安装tomcat服务, 启动停止tomcat服务
在项目开发过程中,以前只是在Eclipse中配置.启动.停止tomcat服务器 如果只想在机器中使用tomcat服务器,而不想安装MyEclipse,可以使用service.bat 将tomcat安装 ...
- web站点和windows服务项目发布时如何排除指定文件
在发布asp.net站点和windows服务项目时,有的时候这样的需求:msbuild编译之后发布到服务器指定目录时要排除指定文件,比如通过jenkins构建时,不希望覆盖原来的Web.config和 ...
- 【OF框架】使用OF.WinService项目,添加定时服务,进行创建启动停止删除服务操作
准备 使用框架搭建完成项目,包含OF.WinService项目. 了解Window Service 和定时服务相关知识. 一.添加一个定时服务 第一步:了解项目结构 第二步:创建一个新的Job 第三步 ...
随机推荐
- VS2013配置编译Caffe-Win10_X64
原文链接:http://blog.csdn.net/joshua_1988/article/details/45036993 有少量修改................ 2014年4月的时候自己在公司 ...
- spring cloud(五) hystrix
开启feign 熔断 hystrix 整合hystrix-dashboard监控面板 1. 服务调用者boot工程 pom引入依赖 <!-- hystrix-dashboard 监控依赖 ...
- eas中删除原来的监听事件添加新的监听事件
kdtEntrys.removeKDTEditListener(kdtEntrys.getListeners(KDTEditListener.class)[0]); kdtEntrys.addKDT ...
- html第三节课
表单 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...
- 宏、预编译(day12)
指针数组里的每个存储区是一个指针类型 的存储区 字符指针数组里包含多个字符类型指针,其中 每个指针可以表示一个字符串 字符指针数组可以用来表示多个相关字符串 主函数的第二个参数是一个字符指针数组, 其 ...
- linux -- 扩容 /home 空间( xfs文件系统分区扩容指定挂载点)
问题: /home空间容量不够使用,扩容卷组,扩容挂载点 方法: 1. 确认有可用的物理磁盘 fdisk -l -- 查看磁盘信息 df -h -- 查看当前挂载信息 vgs -- 查看当前卷组信息 ...
- elementUI 时间选择器,时间选择快捷键
elementUI的时间快捷键,使用属性:picker-options="pickerOptions",由于pickerOptions的定义很长,也在其他地方共用,因此建议提取出来 ...
- jvm学习-ClassLoader(二)
ClassLoader结构 jdk加载的4个步骤 CustomClassLoader 用户自定义的classLoader APPClassLoader主要加载classPath下面的class Ext ...
- 洛谷 P1640 BZOJ 1854 [SCOI2010]连续攻击游戏
题目描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备 ...
- magento 在全站或者某页面增加外联js或者css的方法
1,在整站末尾增加外联资源 找到当前主题的布局文件cms.xml,在<default></default>添加如下代码: <reference name="be ...