C# 创建Windows Service
2015-08-18 21:21
585 查看
当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现。这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Service。
新建Windows Service项目:
删除自动生成的Service1.cs文件,新建WindowsService类,继承ServiceBase。
新建WindowsServiceInstaller类,添加System.Configuration.Install引用,
编译项目,此时编译完的CSWindowsService.exe运行后提示:
我们需要通过installutil.exe来部署Windows Service。
以管理员身份运行Developer Command Prompt。部署命令:installutil /i 文件路径。
例如:installutil /i D:\CSWindowsService.exe
卸载命令 installutil /u 文件路径。
到这里,一个简单的Windows Serive就讲完了。
关于Windows Service更多的内容,请参考MSDN文档。
感谢您的阅读,代码点击这里下载。
新建Windows Service项目:
删除自动生成的Service1.cs文件,新建WindowsService类,继承ServiceBase。
class WindowsService : ServiceBase { public WindowsService() { this.ServiceName = "Test Windows Service"; this.EventLog.Log = "Application"; this.CanHandlePowerEvent = true; this.CanHandleSessionChangeEvent = true; this.CanPauseAndContinue = true; this.CanShutdown = true; this.CanStop = true; } #region // 可以把需求实现代码放置在重写方法内 protected override void Dispose(bool disposing) { base.Dispose(disposing); } protected override void OnStart(string[] args) { base.OnStart(args); } protected override void OnStop() { base.OnStop(); } protected override void OnPause() { base.OnPause(); } protected override void OnContinue() { base.OnContinue(); } protected override void OnShutdown() { base.OnShutdown(); } protected override void OnCustomCommand(int command) { base.OnCustomCommand(command); } protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) { return base.OnPowerEvent(powerStatus); } protected override void OnSessionChange(SessionChangeDescription changeDescription) { base.OnSessionChange(changeDescription); } #endregion }
新建WindowsServiceInstaller类,添加System.Configuration.Install引用,
[RunInstaller(true)] class WindowsServiceInstaller : Installer { public WindowsServiceInstaller() { ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller(); ServiceInstaller serviceInstaller = new ServiceInstaller(); serviceProcessInstaller.Account = ServiceAccount.LocalSystem; serviceProcessInstaller.Username = null; serviceProcessInstaller.Password = null; serviceInstaller.DisplayName = "CSharp Windows Service"; serviceInstaller.StartType = ServiceStartMode.Automatic; serviceInstaller.ServiceName = "Windows Service"; this.Installers.Add(serviceProcessInstaller); this.Installers.Add(serviceInstaller); } }
编译项目,此时编译完的CSWindowsService.exe运行后提示:
我们需要通过installutil.exe来部署Windows Service。
以管理员身份运行Developer Command Prompt。部署命令:installutil /i 文件路径。
例如:installutil /i D:\CSWindowsService.exe
卸载命令 installutil /u 文件路径。
到这里,一个简单的Windows Serive就讲完了。
关于Windows Service更多的内容,请参考MSDN文档。
感谢您的阅读,代码点击这里下载。
相关文章推荐
- C# 读取文本文档(转)
- C#在WinForm中重写ProgressBar控件(带%的显示)
- C# WPF 动态添加窗体
- C# 窗体间传值方法大汇总(转)
- c# 下三角实现 九九乘法口诀表
- c#值类型与引用类型
- C# 实现ADSL自动断网和拨号(适用于拨号用户)
- C#访问Azure的资源
- C# indexof和indexofany区别(转)
- c# GDI+ bitmap截屏不清晰的问题
- C#中string类的几个方法(indexof、lastindexof、substring)(转)
- C#使用IComparer自定义List类实现排序的方法
- 经验28--相关时间戳,C#
- C#自定义基于控制台的Timer实例
- 【面向对象】C#中的面向对象
- c#设计模式-总结(针对GOF23)
- c# GDI画圆
- 问题:C#Chart控件自动添加Series;结果:图形组件Chart动态添加Series
- c#基础之数组
- c#基础之数组