自定义Windows服务并实施安装

1、新建项目DemoService,并添加windows服务,命名DemoService

2、添加代码 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Diagnostics;
 6 using System.Linq;
 7 using System.ServiceProcess;
 8 using System.Text;
 9 using System.Configuration;
10 using System.Windows.Forms;
11 using System.Threading;
12 
13 namespace DemoService
14 {
15     public partial class DemoService : ServiceBase
16     {
17         private System.Timers.Timer timer1;
18         public DemoService()
19         {
20             InitializeComponent();
21             this.timer1 = new System.Timers.Timer();
22             this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
23         }
24 
25         protected override void OnStart(string[] args)
26         {
27             System.Threading.Thread.Sleep(10000);
28             LogUtil.WriteLog("    服务开始启动");
29             this.timer1.Interval = 1000 * Convert.ToInt32(ConfigurationManager.AppSettings["TimeSpan"]);
30             this.timer1.Enabled = true;
31             //this.timer1.Start();
32             LogUtil.WriteLog("    服务启动完成");
33         }
34 
35         protected override void OnStop()
36         {
37             LogUtil.WriteLog("    服务正在停止");
38             this.timer1.Stop();
39             this.timer1.Enabled = false;
40             LogUtil.WriteLog("    服务已经停止");
41         }
42 
43         private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
44         {
45             LogUtil.WriteLog("    服务测试运行中...");
46             Thread.Sleep(10000);
47             MonitorService.ShouldRestart = true;
48         }
49     }
50 }

3、添加安装类,并配置如下:

 4、配置App.config:

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3     <appSettings>
 4         <!--重启超时时间 单位:秒-->
 5         <add key="RestartTimeOut" value="3600"/>
 6         <!--执行时间间隔 单位:秒-->
 7         <add key="TimeSpan" value="5"/>
 8         <add key="LogPath" value="c:\ProjectService\LOG\"/>
 9     </appSettings>
10 </configuration>


5、LogUtil.cs

public class LogUtil
{
	public static void WriteLog(string error)
	{
		string fileName = DateTime.Now.ToString("yyyy-MM-dd") + "DomainServiceLog.txt";
		using (System.IO.StreamWriter sw = new System.IO.StreamWriter(ConfigurationManager.AppSettings["LogPath"] + fileName, true))
		{
			sw.WriteLine("---------------------------------------------------------");
			sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + error);
		}
	}

}

 6、生成项目,执行如下脚本安装服务:

@echo off
set path=C:WindowsMicrosoft.NETFrameworkv4.0.30319

@echo 服务安装中...

InstallUtil.exe C:ProjectServiceDemoService.exe

@echo 服务安装完成.

pause

 执行如下脚本启动服务:

@echo off

if not exist C:\ProjectService\LOG\ (
	md C:\ProjectService\LOG\
)

@echo 服务启动中...

net start DemoService

@echo 服务正在运行...

pause

 执行如下脚本停止服务:

@echo off

@echo 服务停止中...

net stop DemoService

@echo 服务已经停止.

pause

 执行如下脚本卸载服务:

@echo off
set path=C:WindowsMicrosoft.NETFrameworkv4.0.30319

@echo 服务卸载中...

InstallUtil.exe /u C:ProjectServiceDemoService.exe

@echo 服务卸载完成.

pause

 7、如果把命令写在一起,可以根据选择进行操作,命令如下:

@echo off 
@echo **********************************************

rem 关闭自动输出

:begin

echo 请输入 1:安装服务 2:启动服务 3:停止服务 4:卸载服务 其他:退出
rem 接收输入
set input=
set /p input=

rem 输出得到的输入信息

rem echo 您输入的字符串是:%input%
if %input%==1 (
	set path=C:WindowsMicrosoft.NETFrameworkv4.0.30319
	@echo 服务安装中...
	InstallUtil.exe C:ProjectServiceDemoService.exe
	@echo 服务安装完成.
	goto begin
)
if %input%==2 (
	set path=C:WindowsSystem32
	if not exist C:\ProjectService\LOG\ (
		md C:\ProjectService\LOG\
	)
	@echo 服务启动中...
	net start DemoService
	net start MonitorService
	@echo 服务正在运行...
	goto begin
)
if %input%==3 (
	set path=C:WindowsSystem32
	@echo 服务停止中...
	net stop DemoService
	net stop MonitorService
	@echo 服务已经停止.
	goto begin
)
if %input%==4 (
	set path=C:WindowsMicrosoft.NETFrameworkv4.0.30319
	@echo 服务卸载中...
	InstallUtil.exe /u C:ProjectServiceDemoService.exe
	@echo 服务卸载完成.
	goto begin
)

goto end

rem pause>null

rem echo.

rem 从begin标签出,再次运行

rem goto begin

:end

@echo **********************************************

完整代码:DemoService.rar

原文地址:https://www.cnblogs.com/ziranquliu/p/4800725.html