C# 代码重启windows服务
ServiceController service = new ServiceController("EnergyRecordService");
protected void btnRestart_Click(object sender, EventArgs e)
{
try
{
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
}
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
catch (Exception)
{
}
}
Web.Config 配置文件:
如果配置文件不配置这个的话会报一个错误:
Win32Exception (0x80004005): Access is denied
[InvalidOperationException: Cannot open EnergyRecordService service on computer '.'.]
<system.web> <identity impersonate="true" userName="服务器登录名" password="登录密码"/> </system.web>
这个例子展示了如何用C#编程实现启动、停止和重启Windows服务。
启动服务
下面的方法尝试通过指定的服务名称启动服务。然后等待知道服务运行或发生超时。
public static void StartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
// ...
}
}
public static void StopService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
catch
{
// ...
}
}
C# 代码重启windows服务的更多相关文章
- C#代码安装Windows服务(控制台应用集成Windows服务)
最近在为一款C/S架构的科研软件开发云计算版,需要用到WCF,考虑到不需要什么界面以及稳定性,无人值守性,准备用Windows Service作为宿主,无奈Windows Service的安装太为繁复 ...
- 通过C#来开启、关闭、重启Windows服务
通过C#开启服务需要这个C#程序有相应权限,比如服务的账户是Local System的就必须以管理员权限运行C#程序才能开启或关闭. 这里只写重启的方式(就是先关闭,后开启): // Security ...
- C#代码安装Windows服务
using System;using System.Collections.Generic;using System.ServiceProcess;using System.Configuration ...
- ASP.NET 下使用特定身份完成windows服务的功能操作
今天部署项目的发现一个问题: 在本地Win7系统下利用Web页面完成Windows服务的功能操作(启动.停止.安装.卸载)都是正常的,而部署到Server2008系统下,再使用Web页面完成windo ...
- 第十三篇 一个安装、管理windows服务的桌面程序
在网上看到一个修改程序入口的程序去把windows 服务修改成控制台的程序,然后利用控制台的程序把服务安装和管理,也想起自己原来也写了一个对windows 报务管理的程序,不过是winform的. ...
- [转帖]以Windows服务方式运行.NET Core程序
以Windows服务方式运行.NET Core程序 原作者blog:https://www.cnblogs.com/guogangj/p/10093102.html 里面使用了NSSM 工具 但是自己 ...
- 连表查询都用Left Join吧 以Windows服务方式运行.NET Core程序 HTTP和HTTPS的区别 ASP.NET SignalR介绍 asp.net—WebApi跨域 asp.net—自定义轻量级ORM C#之23中设计模式
连表查询都用Left Join吧 最近看同事的代码,SQL连表查询的时候很多时候用的是Inner Join,而我觉得对我们的业务而言,99.9%都应该使用Left Join(还有0.1%我不知道在 ...
- .net core 部署到windows服务上的方法
前言 Net core 项目部门在Windows有很多种方式,大致有以下几种, dotnet 命令, iis(windowshosts), 一些开源的应用容器(docker ) 基于一些exe 程序, ...
- C#创建、安装一个Windows服务
关于WIndows服务的介绍,之前写过一篇: http://blog.csdn.net/yysyangyangyangshan/article/details/7295739.可能这里对如何写一个服务 ...
随机推荐
- 翻译:Angular 2 - TypeScript 5 分钟快速入门
原文地址:https://angular.io/docs/ts/latest/quickstart.html Angular 2 终于发布了 beta 版.这意味着正式版应该很快就要发布了. 让我们使 ...
- MFC学习 标签页与属性页及各常用控件使用
参考 http://blog.csdn.net/anye3000/article/details/6700023 CTabCtrl: BOOL CTabTestDlg::OnInitDialog() ...
- Win2008远程多用户登陆的配置方法 另附详细设置: Windows server 2008 R2实现多用户远程连接
Win2008远程多用户登陆的配置方法 在使用Windows 2008远程登录功能时,如果需要进行多用户登录,可以采用以下配置方法: 首先要启用远程桌面这一功能:右击“我的电脑”→ 属性 → ...
- C/C++笔试经典程序(二)
1.下面5个函数哪个能够成功进行两个数的交换? swap1传的是值的副本,在函数体内被修改了形参p.q(实际参数a.b的一个拷贝),p.q的值确实交换了,但是它们是局部变量,不会影响到主函数中的a和b ...
- university, school, college, department, institute的区别
这些个词没有太大区别,有时候有些词是可以通用的,而有些用法则是随着地域时间的不同而变迁. 一般说来,college译作“学院”,它是university (综合性大学)的一个组成部分,例如,一所综合大 ...
- Android开发-API指南-<application>
<application> 英文原文:http://developer.android.com/guide/topics/manifest/application-element.html ...
- 如何在Flash中新窗口打开页面而不被拦截
Flash的wmode必须是opaque或者transparent,允许Flash访问页面脚本.另外跳转必须是点击直接触发. 代码:ExternalInterface.call("windo ...
- 阿里云搭建LAMP环境详细教程
一.云服务器的选择 随着大数据时代的到来,为了满足更为复杂的运算和用户更为苛刻的需求,云计算应运而生,随之而来的就是云服务器.过去的服务器成本较高,运算模式较为单一,资源利用率不高,而云服务器的出现刚 ...
- 常用上传shell脚本
#!/bin/bash FTP_IP=127.0.0.1 FTP_USER="aaa" FTP_PSW="bbb" GAME_NAME="ccc&qu ...
- java.lang.IllegalArgumentException
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Persiste ...