C#开发Windows服务的基础代码
做项目需要对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服务的基础代码的更多相关文章
- 使用Visual Studio 2015 Community 开发windows服务
昨天研究在.NET下开发Windows服务程序,期间遇到一些小问题,这里仅将自己的开发过程和需要注意的地方写下和广大网友分享…… 1.基础 Windows服务是指系统启动时能够自己运行的程序.W ...
- C#开发Windows服务 附简单实例实现禁止QQ运行
本实例主要实现下面三个基本功能 1.C#开发windows服务 2.禁止QQ等程序运行 3.为windows服务创建自动安装程序 下面针对这三个基本功能进行实现 一.C#开发windows服务 Win ...
- VS2013开发Windows服务项目
这篇随笔里,我将介绍如何用VS2013开发Windows服务项目,实现的功能是定时发送电子邮件. 开发环境:VS2013,SQL Server2008,采用C#语言开发 步骤一:创建Windows服务 ...
- C#开发Windows服务详细流程
1.Windows服务简单介绍 Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序,主要用于长时间运行的功能或者执行定时任务.一般情况下,用户不能通过用户界面来安装和启 ...
- 使用Topshelf开发Windows服务、log4net记录日志
开发windows服务,除了在vs里新建服务项目外(之前有写过具体开发方法,可点击查看),还可以使用Topshelf. 不过使用topshelf需要.netframework 4.5.2版本,在vs2 ...
- c#金额转换成中文大写金额 .Net开发Windows服务
c#金额转换成中文大写金额 2018-08-24 转别人 c#金额转换成中文大写金额 /// <summary> /// 金额转换成中文大写金额 /// </summary> ...
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
C# DateTime的11种构造函数 别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...
- C#开发windows服务如何调试——资料整理
原文标题:C# Windows服务程序如何进行调试 原文地址:https://jingyan.baidu.com/article/456c463b18e1b00a583144b3.html 第一种: ...
- C#常规开发Windows服务
.Net平台下开发Windows服务的支持库很多,除了通过标准的Windows服务项目,还有一些优秀的开源架构比如:TopSelf:本文以常规项目为例 一.开发 1.新建[Windows服务] 项目: ...
随机推荐
- 一次外企QQ面试
无忧上挂了简历,让个外企的hr约好面试,今天刚面完,整理出来给大家看看.难度不是很大,基本就是Asp.net Mvc 用到的东西,没有问数据库方面的. Part I – Frontend 1. Tr ...
- ECMAScript 6中的let和const关键词
ECMAScript 6中多了两个定义变量的关键词,一个是let,另一个是const,后者顾名思义就是常量定义,前者的作用域范围是块级的. 一般写过js的童鞋都知道,同其他语言一样,JS中的变量作用域 ...
- Etag缓存在PHP和NodeJS中的实现
HTTP 提供了许多页面缓存的方案,其中属 Etag 和 Last-Modified 应用最广.本文会先介绍 Etag 的应用场景,然后说说他在 php 和 node 中的使用. 本文地址:http: ...
- 架构设计:前后端分离之Web前端架构设计
在前面的文章里我谈到了前后端分离的一些看法,这个看法是从宏观的角度来思考的,没有具体的落地实现,今天我将延续上篇文章的主题,从纯前端的架构设计角度谈谈前后端分离的一种具体实现方案,该方案和我原来设想有 ...
- [PCB制作] 1、记录一个简单的电路板的制作过程——四线二项步进电机驱动模块(L6219)
前言 现在,很多人手上都有一两个电子设备,但是却很少有人清楚其中比较关键的部分(PCB电路板)是如何制作出来的.我虽然懂点硬件,但是之前设计的简单系统都是自己在万能板上用导线自己焊接的(如下图左),复 ...
- undefined function openssl_x509_read
打开php.ini,找到这一行 ;extension=php_openssl.dll,将前面的";"去掉 再重启apache或者iis即可
- 阿里巴巴B2B搜索学习
1.搜索业务 主搜索:商品搜索.商家搜索.采购搜索.app搜索 行业搜索:淘货源.淘工厂.聚好货.主题市场.品牌馆等 2.优势 由于用户多,需求强烈,收益大,所以功能.场景.架构做到极致高效. 代码复 ...
- struts2学习笔记之十三:自定义过滤器
Struts2的拦截器 1.Struts2的拦截器只能拦截Action,拦截器是AOP的一种思路,可以使我们的系统架构 更松散(耦合度低),可以插拔,容易互换,代码不改变的情况下很容易满足客户需求 其 ...
- javascript中this指向
在简单函数中,this是指向当前对象,可用来获取当前对象某个属性,但随着函数变复杂,this很多情况不指向当前对象,而是指向window. 1.在独立调用函数中,具有全局执行环境,this指向wind ...
- 如何使用ITEXTSHARP将HTML代码字符串写进PDF
原文 如何使用ITEXTSHARP将HTML代码字符串写进PDF itextsharp包括一个简单的类,可以用来根据html代码或字符串创建pdf文件.使用此类,你可以使用短短几行代码,就将 HTML ...