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服务的更多相关文章

  1. C#代码安装Windows服务(控制台应用集成Windows服务)

    最近在为一款C/S架构的科研软件开发云计算版,需要用到WCF,考虑到不需要什么界面以及稳定性,无人值守性,准备用Windows Service作为宿主,无奈Windows Service的安装太为繁复 ...

  2. 通过C#来开启、关闭、重启Windows服务

    通过C#开启服务需要这个C#程序有相应权限,比如服务的账户是Local System的就必须以管理员权限运行C#程序才能开启或关闭. 这里只写重启的方式(就是先关闭,后开启): // Security ...

  3. C#代码安装Windows服务

    using System;using System.Collections.Generic;using System.ServiceProcess;using System.Configuration ...

  4. ASP.NET 下使用特定身份完成windows服务的功能操作

    今天部署项目的发现一个问题: 在本地Win7系统下利用Web页面完成Windows服务的功能操作(启动.停止.安装.卸载)都是正常的,而部署到Server2008系统下,再使用Web页面完成windo ...

  5. 第十三篇 一个安装、管理windows服务的桌面程序

    在网上看到一个修改程序入口的程序去把windows 服务修改成控制台的程序,然后利用控制台的程序把服务安装和管理,也想起自己原来也写了一个对windows 报务管理的程序,不过是winform的.   ...

  6. [转帖]以Windows服务方式运行.NET Core程序

    以Windows服务方式运行.NET Core程序 原作者blog:https://www.cnblogs.com/guogangj/p/10093102.html 里面使用了NSSM 工具 但是自己 ...

  7. 连表查询都用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%我不知道在 ...

  8. .net core 部署到windows服务上的方法

    前言 Net core 项目部门在Windows有很多种方式,大致有以下几种, dotnet 命令, iis(windowshosts), 一些开源的应用容器(docker ) 基于一些exe 程序, ...

  9. C#创建、安装一个Windows服务

    关于WIndows服务的介绍,之前写过一篇: http://blog.csdn.net/yysyangyangyangshan/article/details/7295739.可能这里对如何写一个服务 ...

随机推荐

  1. (转)WebApi自动生成在线文档Swashbuckle

    原文地址:http://www.cnblogs.com/Arrays/p/5146194.html?utm_source=tuicool&utm_medium=referral 1.前言 1. ...

  2. FAQ: Automatic Statistics Collection (文档 ID 1233203.1)

    In this Document   Purpose   Questions and Answers   What kind of statistics do the Automated tasks ...

  3. Oracle Grid Infrastructure: Understanding Split-Brain Node Eviction (文档 ID 1546004.1)

    In this Document   Purpose   Scope   Details   What does "split brain" mean?   Why is this ...

  4. MapHttpRoute

    http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Http/HttpRouteCollectionExten ...

  5. GitHub指南

    1.创建新仓库 #创建新文件夹,打开,然后执行 git init #以创建新的 git 仓库. 2.检出仓库 #执行如下命令以创建一个本地仓库的克隆版本: git clone /path/to/rep ...

  6. 跟我学 NHibernate (一)

    NHibernate 是一个强大的 ORM 框架,本博文主要就 NHibernate 的使用方法及语法做一些简单的介绍.  1.NHibernate 语法   新建一个类,命名为: QueryCrit ...

  7. python中若干错误

    今天在运行的django的时候一直提示”系统错误“,如下 except Exception, ex: logger.error(printException()) return render_stri ...

  8. 动态链接库(dll) __declspec(dllimport) __declspec(dllexport)

    一. __declspec(dllexport) Microsoft 在 Visual C++ 的 16 位编译器版本中引入了 __export,使编译器得以自动生成导出名并将它们放到一个 .lib ...

  9. ASP.NET MVC 开源项目学习之ProDinner (三)

    第四层:Resources   这一层里面主要是几个资源文件. 资源文件知识小杂烩: 几乎每一个生产性应用程序都需要使用资源.资源是在逻辑上由应用程序部署的任何非可执行数据.资源可以在应用程序中作为错 ...

  10. 图解CISCO 3550忘记密码解决方法

    图解CISCO3550忘记密码解决方法 Cisco网络设备密码忘记怎么初始出厂默认值?这时网友常常会提出的问题,怎么解决,有人说啦,去百度去谷歌一下不就行啦,然而这对初学者任是个挑战,虽然步骤只有简单 ...