C#开发Windows服务的基础代码
2014-02-16 00:23
274 查看
做项目需要对Windows服务进行操作,从网上找了一些资料,总结如下:
(以下程序在程序中测试通过)
博客园 柔城 以上代码概述:
C#对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 = 0; i < 30; i++) { service.Refresh(); System.Threading.Thread.Sleep(1000); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running) { break; } if (i == 29) { 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 = 0; i < 30; i++) { service.Refresh(); System.Threading.Thread.Sleep(1000); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped) { break; } if (i == 29) { throw new Exception("服务" + serviceName + "停止失败!"); } } } } } } }
博客园 柔城 以上代码概述:
C#对Windows服务操作(注册安装服务,卸载服务,启动停止服务,判断服务存在)
相关文章推荐
- C#开发Windows服务的基础代码
- C#开发Windows服务的基础代码
- C# 微信服务号开发基础 完整代码
- C#代码安装Windows服务(控制台应用集成Windows服务)
- C#基础系列:开发自己的窗体设计器(在容器上拖动鼠标增加控件)
- Android-NDK开发之基础--Android JNI实例代码(二)-- 获取/设置类的域或者说属性字段
- arcgis二次开发时,c#代码实现鼠标点击地图时,获取地图的表的字段的信息
- C#开发中使用Npoi操作excel实例代码
- C#使用Xamarin开发可移植移动应用进阶篇(9.混淆代码,防止反编译)
- C#上传图片代码|.NET快速开发平台|.NET智能表单|.NET工作流
- 毕业生的商业软件开发之路 --- C#基本语法-C#代码模块逻辑框架
- (原创)利用vs.net快速开发windows服务(c#)
- 爬虫技术 -- 基础学习(五)解决页面编码识别(附c#代码)
- C#开发在VS2010开发环境里显示代码行数
- C#之网页开发基础
- 使用VS2010 C#开发ActiveX控件(下),完整代码打包下载
- iOS基础开发UI界面-xib与代码自定义控件的区别
- C#.NET常见问题(FAQ)-使用SharpDevelop开发 如何切换设计视图和代码视图
- vs2008 windows服务(C#) 开发与安装
- iOS开发之UI基础--纯代码控件.frame,center,bounds属性