当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现。这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Service。

新建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# 创建Windows Service的更多相关文章

  1. C#创建Windows Service(Windows 服务)基础教程

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  2. 创建Windows Service

    基本参照使用C#创建Windows服务,添加了部分内容 目录 创建Windows Service 可视化管理Windows Service 调试 示例代码 创建Windows Service 选择C# ...

  3. C# 创建Windows Service(Windows服务)程序

    本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...

  4. .NET 6学习笔记(2)——通过Worker Service创建Windows Service

    通过Visual Studio中的Windows Service模板,我么可以创建.NET Framework版本的Windows Service,网络上对此已有详细且丰富的各路教程.但在我们升级到. ...

  5. 目前.NET Core创建Windows Service比较好的一个开源框架:DasMulli.Win32.ServiceUtils

    新建一个.NET Core控制台程序,搜索并下载Nuget包:DasMulli.Win32.ServiceUtils GitHub 链接及使用指南 Write a windows service us ...

  6. 通过TopShelf简单创建windows service

    目前很多项目都是B/S架构的,我们经常会用到webapi.MVC等框架,实际项目中可能不仅仅是一些数据的增删改查,需要对数据进行计算,但是将计算逻辑放到api层又会拖累整个项目的运行速度,从而会写一些 ...

  7. 使用.net 创建windows service

    最近公司项目需要,写了个windows 服务,windows 服务的内容可以在VS 中新建一个"windows服务项目", (1)服务中的主要代码: public partial ...

  8. 【C#】C#创建Windows Service服务

    目录结构: contents structure [+] 创建Windows服务 配置 安装Windows服务 在Visual Studio中调试 常见问题 最近写了一个TCP连接的程序,由于这种通信 ...

  9. VS2010 创建 windows service 程序

    参考网上保护眼睛程序,自写程序如下. 1.创建一个名词为“CareEyeService”,类型为“WindowsService”的应用程序. 自动生成代码如下图: 2.修改ServiceCareEye ...

随机推荐

  1. C#调用有有参有返的存储过程

    (1)在SQL Server中建立如下的存储过程: set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOCREATE PROCEDURE [dbo].[GetName ...

  2. MySql 导出excel

    select * into outfile './bestlovesky.xls' from bestlovesky where 1 order by id desc  limit 0, 50;

  3. c3p0 config

    c3p0-config.xml<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> ...

  4. Java RuntimeException异常处理汇总

    Java中所有异常的父类是Throwable类,在Throwable类下有两大子类: 一个是Error类,指系统错误异常,例如:VirtualMachineError 虚拟机错误,ThreadDeat ...

  5. the XMLHttpRequest Object

    Ajax的核心是XMLHttpRequest对象.XMLHttpRequest对象允许异步发送请求并且以文本的形式返回结果.发送什么样的请求(what),在服务器端怎样处理这个请求(how),返回什么 ...

  6. Java中时间日期格式化

    1.与日期时间相关的类:      第一:java.util.Date;                           将时间作为一个整体使用.处理时,使用Date类较为简便      第二:j ...

  7. 14. javacript高级程序设计-表单

    1. 表单脚本 1.1 基础知识 <from>元素表示表单: l acceptCharset:服务器能处理的字符集 l action:接受请求的URL l elements:表单中所有控件 ...

  8. ALV 输入数据时数字缩小一千倍的问题解决

    这个字段后面有三位小数  我输入 1 一回车 就变成了0.001 了 解决方法 DATA: wa_fieldcatalog    TYPE lvc_s_fcat,    "显示数据列内表工作 ...

  9. NSBundle的pathForResource:ofType: 返回值为nil问题

    一.问题描述 在处理XML的过程中,在项目中手动创建名为“data.xml”的文件,加入网上找的内容 <?xml version="1.0" encoding="u ...

  10. 自定义tld标签,页面使用

    背景需求: 系统本身的session不能在页面使用 如下: controller: @RequestMapping(method=RequestMethod.GET) public String ge ...