做项目需要对Windows服务进行操作,从网上找了一些资料,总结如下:

(以下程序在程序中测试通过)

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Configuration.Install;
using System.Collections.Specialized;
using System.ServiceProcess; namespace IAU.ServerInstall.BLL
{
public class ServiceControl
{
/// <summary>
/// 注册服务(注册完就启动,已经存在的服务直接启动。)
/// </summary>
/// <param name="strServiceName">服务名称</param>
/// <param name="strServiceInstallPath">服务安装程序完整路径(.exe程序完整路径)</param>
public void Register(string strServiceName, string strServiceInstallPath)
{
IDictionary mySavedState = new Hashtable(); try
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(strServiceName); //服务已经存在则卸载
if (ServiceIsExisted(strServiceName))
{
//StopService(strServiceName);
UnInstallService(strServiceName, strServiceInstallPath);
}
service.Refresh();
//注册服务 http://www.cnblogs.com/sosoft/
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); mySavedState.Clear();
myAssemblyInstaller.Path = strServiceInstallPath;
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Install(mySavedState);
myAssemblyInstaller.Commit(mySavedState);
myAssemblyInstaller.Dispose(); service.Start();
}
catch (Exception ex)
{
throw new Exception("注册服务时出错:" + ex.Message);
}
} /// <summary>
/// 卸载服务
/// </summary>
/// <param name="strServiceName">服务名称</param>
/// <param name="strServiceInstallPath">服务安装程序完整路径(.exe程序完整路径)</param>
public void UnInstallService(string strServiceName, string strServiceInstallPath)
{
try
{
if (ServiceIsExisted(strServiceName))
{
//UnInstall Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = strServiceInstallPath;
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
}
}
catch (Exception ex)
{
throw new Exception("卸载服务时出错:" + ex.Message);
}
} /// <summary>
/// 判断服务是否存在
/// </summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
return true;
}
}
return false;
} /// <summary>
/// 启动服务(启动存在的服务,30秒后启动失败报错)
/// </summary>
/// <param name="serviceName">服务名</param>
public void StartService(string serviceName)
{
if (ServiceIsExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
{
service.Start();
for (int i = ; i < ; i++)
{
service.Refresh();
System.Threading.Thread.Sleep();
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
break;
}
if (i == )
{
throw new Exception("服务" + serviceName + "启动失败!");
}
}
}
}
} /// <summary>
/// 停止服务(停止存在的服务,30秒后停止失败报错)
/// </summary>
/// <param name="serviceName"></param>
public void StopService(string serviceName)
{
if (ServiceIsExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
service.Stop();
for (int i = ; i < ; i++)
{
service.Refresh();
System.Threading.Thread.Sleep();
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
break;
}
if (i == )
{
throw new Exception("服务" + serviceName + "停止失败!");
}
}
}
}
}
}
}

博客园 柔城  以上代码概述:

C#对Windows服务操作(注册安装服务,卸载服务,启动停止服务,判断服务存在)

C#开发Windows服务的基础代码的更多相关文章

  1. 使用Visual Studio 2015 Community 开发windows服务

    昨天研究在.NET下开发Windows服务程序,期间遇到一些小问题,这里仅将自己的开发过程和需要注意的地方写下和广大网友分享……  1.基础   Windows服务是指系统启动时能够自己运行的程序.W ...

  2. C#开发Windows服务 附简单实例实现禁止QQ运行

    本实例主要实现下面三个基本功能 1.C#开发windows服务 2.禁止QQ等程序运行 3.为windows服务创建自动安装程序 下面针对这三个基本功能进行实现 一.C#开发windows服务 Win ...

  3. VS2013开发Windows服务项目

    这篇随笔里,我将介绍如何用VS2013开发Windows服务项目,实现的功能是定时发送电子邮件. 开发环境:VS2013,SQL Server2008,采用C#语言开发 步骤一:创建Windows服务 ...

  4. C#开发Windows服务详细流程

    1.Windows服务简单介绍 Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序,主要用于长时间运行的功能或者执行定时任务.一般情况下,用户不能通过用户界面来安装和启 ...

  5. 使用Topshelf开发Windows服务、log4net记录日志

    开发windows服务,除了在vs里新建服务项目外(之前有写过具体开发方法,可点击查看),还可以使用Topshelf. 不过使用topshelf需要.netframework 4.5.2版本,在vs2 ...

  6. c#金额转换成中文大写金额 .Net开发Windows服务

    c#金额转换成中文大写金额   2018-08-24 转别人 c#金额转换成中文大写金额 /// <summary> /// 金额转换成中文大写金额 /// </summary> ...

  7. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  8. C#开发windows服务如何调试——资料整理

    原文标题:C# Windows服务程序如何进行调试 原文地址:https://jingyan.baidu.com/article/456c463b18e1b00a583144b3.html 第一种: ...

  9. C#常规开发Windows服务

    .Net平台下开发Windows服务的支持库很多,除了通过标准的Windows服务项目,还有一些优秀的开源架构比如:TopSelf:本文以常规项目为例 一.开发 1.新建[Windows服务] 项目: ...

随机推荐

  1. Guava - EventBus(事件总线)

    Guava在guava-libraries中为我们提供了事件总线EventBus库,它是事件发布订阅模式的实现,让我们能在领域驱动设计(DDD)中以事件的弱引用本质对我们的模块和领域边界很好的解耦设计 ...

  2. js中各种跨域问题实战小结(一)

    什么是跨域?为什么要实现跨域呢? 这是因为JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.也就是说只能访问同一个域中的资源.我觉得这就有必要了解下javascript中的同源策略 ...

  3. [nRF51822] 2、D-BUG之诗

    4线SPI彩屏局部刷屏偏移解决 ——原来我早已经在成功的旁边了 最近在研究用低速.低RAM的单片机来驱动小LCD或TFT彩屏实现动画效果 首先我用一个16MHz晶振的m0内核的8位单片机nRF5182 ...

  4. 《OOC》笔记(0)——为何要看这本书

    <OOC>笔记(0)——为何要看这本书 <OOC>全名是<Object-oriented Programming with ANSI-C>,作者Axel-Tobia ...

  5. 一个用微软官方的OpenXml读写Excel 目前网上不太普及的方法。

    新版本的xlsx是使用新的存储格式,貌似是处理过的XML. 传统的excel处理方法,我真的感觉像屎.用Oldeb不方便,用com组件要实际调用excel打开关闭,很容易出现死. 对于OpenXML我 ...

  6. 2015 年最受 Linux 爱好者欢迎的软硬件大盘点

    Linux 爱好者都喜欢用哪些硬件,哪些发行版呢?近日 OpenBenchmarking.org 做了一个 2015 年度数据的统计和梳理,Linux Story 特意整理了一下,分享给大家. 转载于 ...

  7. redis配置文件redis.conf中文版(基于2.4)

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/99.html?1455869981 代码如下: # Redis示例配置文件 ...

  8. Time33算法

    Time33是字符串哈希函数,现在几乎所有流行的HashMap都采用了DJB Hash Function,俗称"Times33"算法.Times33的算法很简单,就是不断的乘33. ...

  9. Atitit 如何利用先有索引项进行查询性能优化

    Atitit 如何利用先有索引项进行查询性能优化 1.1. 再分析的话就是我们所写的查询条件,其实大部分情况也无非以下几种:1 1.2. 范围查找 动态索引查找1 1.2.1. 索引联合 所谓的索引联 ...

  10. Atitit 热更新资源管理器 自动更新管理器 功能设计

    Atitit 热更新资源管理器 自动更新管理器 功能设计 · 多线程并行下载支持 · 两层进度统计信息:文件级以及字节级 · Zip压缩文件支持 · 断点续传 · 详细的错误报告 · 文件下载失败重试 ...