当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用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. 转:jQuery弹出二级菜单

    <html> <head> <meta http-equiv="content-type" content="text/html; char ...

  2. 关闭EXCEL进程

    //导入Windows类库,可以获得进程ID        [DllImport("User32.dll", CharSet = CharSet.Auto)]        pub ...

  3. adb 服务端口2037被占,导致adb和appium无法工作

    症状1: 命令行运行 adb 相关命令,提示如下: adb server is out of date. killing...ADB server didn't ACK* failed to star ...

  4. svn: Commit failed (details follow): svn: Authorization failed

    我的原因是我没有使用账户密码,匿名用户没有写权限,只有只读的权限 修改下svn配置文件中的anon-access=read为anon-access=write 还有一点要注意:选项前面不能留空格,必须 ...

  5. jquery 上传图片即时预览功能

    <script type="text/javascript">        jQuery.fn.extend({            uploadPreview: ...

  6. MST:Out of Hay(POJ 2395)

    缺乏粮草 题目大意:一群牛要修建一些通道,到各个农场距离总和要最小,求这些通道的最大值 水题了,一个Kruskal搞定 #include <iostream> #include <f ...

  7. 中等难度SQL语句(存储过程,分页,拼接字段、游标,日期类型转换,动态行转列,视图)汇总

    一.创建存储过程 if Exists(select name from sysobjects where NAME = 'sp1LoginUser' and type='P')drop procedu ...

  8. UVA 156 Ananagrams ---map

    题目链接 题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词.在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,按字典序进行排列( ...

  9. POJ 1222 EXTENDED LIGHTS OUT (高斯消元)

    题目链接 题意:5*6矩阵中有30个灯,操作一个灯,周围的上下左右四个灯会发生相应变化 即由灭变亮,由亮变灭,如何操作使灯全灭? 题解:这个问题是很经典的高斯消元问题.同一个按钮最多只能被按一次,因为 ...

  10. sqlserver 导出数据字典

    -- 数据字典 SELECT ( then d.name else '' end)表名, a.colorder 字段序号, a.name 字段名, ( then '√'else '' end) 标识, ...