C#编写windows服务

项目要求:

数据库用有一张表,存放待下载文件的地址,服务需要轮训表将未下载的文件下载下来。

表结构如下:

 

过程:

  1. VS--文件-->新建项目-->windows-->windows服务

 

  1. 添加安装程序 Service1.cs设计 右键-->添加安装程序

 

  1. 选中ServiceInstaller1右键 服务名称 填写自定义服务名称

这里填写 MagicFileMonitorService

 

  1. 选中ServiceProcessInstaller1右键 Account 选择 LocalSystem,这里是服务安装时使用的账户

 

  1. 创建安装和卸载文件  Install.bat Uninstall.bat 放在根目录下,并 右键-->属性-->复制到输出目录 选择 始终复制

 

  1. install.bat编写

%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe C:QMDownloadMongoDBFileDowninDebugMongoDBFileDown.exe--这个是exe文件目录

Net Start MagicFileMonitorService--开启服务

sc config MagicFileMonitorService start= auto--将服务设置为自动运行

pause--暂停在cmd窗口中查看安装状态

  1. uninstall.bat编写

%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe /u C:QMDownloadMongoDBFileDowninDebugMongoDBFileDown.exe--这个是exe文件目录

Pause

  1. 在Servic1.cs中添加timer控件,

 

9.加入主体程序(不在详细列出)

10.安装程序

Bin下找到install.bat 右键-->以管理员运行

注意:

1.install.bat一定右键以管理员运行

2.timer1_Elapsed中加入逻辑,一定要加Onstar的内容不然不work

附件(VS2013):2016-07-06

CSharp编写windows服务全过程.rar

MaigcFileMonitor.rar

原文地址:https://www.cnblogs.com/magicsong/p/5647330.html