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.可能这里对如何写一个服务 ...
随机推荐
- oracle插入主键数据、sequence和触发器
一.创建表: id number;并设为主键 name VARCHAR2(20 BYTE) 二. 插入数据 2.1 insert into addservice.test_table (id,na ...
- JDK的版本历史
JDK1.5: 2004年发布----2006年结束 JDK1.6 2006年发布-----2010年结束 一看吓我一条,开始接触JDK都已经是2010年下半年了,可是那是学的就是1.5版本,知道工作 ...
- silverlight,动态数据集合中,移除动态集合自身的内容
在xaml的页面上创建一个x:Name为_list1的ListBox,其中ListBox里面的每一项是ListBoxItem if (_list1.SelectedItem == null)//如果_ ...
- 跨平台c++ Coroutine,仿unity3d实现
不多说,贴代码: #include "stdafx.h" #include <list> #include <thread> #include <ch ...
- memcached学习(5). memcached的应用和兼容程序
mixi在提供服务的初期阶段就使用了memcached. 随着网站访问量的急剧增加,单纯为数据库添加slave已无法满足需要,因此引入了memcached. 此外,我们也从增加可扩展性的方面进行了验证 ...
- 解决visual studio已安装的问题
使用Windows Install Clean Up(用管理员身份打开),找到相应的软件
- html实体字符
在html中,某些字符时预留的,如小于号(<).大于号(>),浏览器会认为它们是标签:有些字符无法用键盘输入.如果需要正确的显示它们,就需要在html源码中使用字符实体. 字符实体有实体名 ...
- 洛谷P2751 [USACO4.2]工序安排Job Processing
P2751 [USACO4.2]工序安排Job Processing 18通过 78提交 题目提供者该用户不存在 标签 难度普及+/提高 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 一家工 ...
- 洛谷P1467 循环数 Runaround Numbers
P1467 循环数 Runaround Numbers 89通过 233提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 循环数是 ...
- SVN与TortoiseSVN实战:补丁详解
硬广:<SVN与TortoiseSVN实战>系列已经写了五篇,第二篇<SVN与TortoiseSVN实战:标签与分支>和第三篇<SVN与TortoiseSVN实战:Tor ...