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 第三步 ...
随机推荐
- ASP.NE 上传文件控件
protected void Button1_Click(object sender, EventArgs e) { //if (Request["id"]==null & ...
- vue.js的ajax和jsonp请求
首先要声明使用ajax 在 router下边的 Index.js中 import VueResource from 'vue-resource'; Vue.use(VueResource); ajax ...
- ESP32 开发笔记(十二)LittlevGL 添加自定义字体和物理按键
LittlevGL 添加自定义字体获取字库 ttf 文件可以从一些网站上获取字库文件,比如请注意字体许可证 生成源文件使用 LittlevGL 提供的字库文件转换工具,将 ttf 字库文件转换为源文件 ...
- 【JavaScript游戏开发】使用HTML5+Canvas+JavaScript 封装的一个超级马里奥游戏(包含源码)
这个游戏基本上是建立在JavaScript模块化的开发基础上进行封装的,对游戏里面需要使用到的游戏场景进行了封装,分别实现了Game,Sprite,enemy,player, base,Animati ...
- 51nod1242斐波那契数列的第N项 【矩阵快速幂】
斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, 13, 21, 34, ...
- 解决windows文件在linux系统中显示乱码的问题
问题: 在Windows下用matlab写的代码(.m)到Linux(centos)下,注释的中文全是乱码. 原因: Windows下默认使用的是GB2312编码,Linux默认使用的是UTF-8. ...
- javascript正则表达式总结(test|match|search|replace|split|exec)
test:测试string是否包含有匹配结果,包含返回true,不包含返回false. <script type="text/javascript"> var str ...
- 继续聊WPF——Expander控件(2)
<Window x:Class="Expander_Sample2.Window1" xmlns="http://schemas.microsoft.com/win ...
- 【codeforces 793C】Mice problem
[题目链接]:http://codeforces.com/contest/793/problem/C [题意] 给你每个点x轴移动速度,y轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...
- 安装Maven并搭建Maven私有仓库
一.说明 Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具.我们在进行Java代码开发的时候,Eclipse+Maven+Jetty是一个十 ...