当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用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. Hibernate 多对多关联查询条件使用

    from Brand as b inner join fetch b.styles as s where s.styleId=?

  2. VQuery选择器

    VQUery elements属性,储存选中的元素 参数 typeof的作用 字符串 class  ID tagName 函数 事件绑定 对象 直接插入 $函数 绑定事件 click方法 显示隐藏,- ...

  3. 应用HTK搭建语音拨号系统3:创建绑定状态的三音素HMM模型

    选自:http://maotong.blog.hexun.com/6261873_d.html 苏统华 哈尔滨工业大学人工智能研究室 2006年10月30日 声明:版权所有,转载请注明作者和来源 该系 ...

  4. ubuntu14.04 163sources.list

    deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse deb http://mirrors.163 ...

  5. git branch使用

    (1) git配置global信息: git config --global user.name "Your Name" git config --global user.emai ...

  6. C++ 使用SQLite

    1.SQLite是一个完全独立的.不需要服务器.不要任何配置.支持SQL的.开源的文件数据库引擎.源代码和支持可以登录:http://www.sqlite.org/   1.1.下载sqlite3.d ...

  7. ABAP 内表的行列转换-发货通知单-打印到Excel里

    需要传入数据到Excel里的模板如上图所示 ********************** *           设计主要逻辑与原理说明                                 ...

  8. Effective C++ -----条款21:必须返回对象时,别妄想返回其reference

    绝不要返回pointer或reference指向一个local stack对象,或返回reference指向一个heap-allocated对象,或返回pointer或reference指向一个loc ...

  9. Html之head部分详解

    随便打开一个网页,右击查看网页源代码,总能看到<head>-</head>封闭标签,在里面通常会包含5类标签:title.link.script.meta.style.这5类标 ...

  10. 【leetcode】atoi (hard) ★

    虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧. 主要是很多情况我都没有考虑到.并且有的时候我的规则和答案中的规则不同. 答案的规则: 1.前导空格全部跳过  “      123” ...