# c#创建 windows 服务
工具:vs2022
# 1、创建 Windows Service 项目
在项目模版里搜索“服务”,选择“Windows 服务(.NET Framework)”
# 2、输入项目代码
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace MonitorService
{
public partial class ServiceMain : ServiceBase
{
public ServiceMain()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WriteLog("启动");
Timer timer = new Timer();
timer.Interval = 60000; // 60 seconds
timer.Elapsed += new ElapsedEventHandler(this.OnTimer);
timer.Start();
}
protected override void OnStop()
{
WriteLog("结束");
}
public void OnTimer(object sender, ElapsedEventArgs args)
{
StopService("wuauserv"); // 定时关闭更新服务
StopService("UsoSvc");
}
private void WriteLog(string message)
{
string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
using (StreamWriter sw = new StreamWriter(path + "Log.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("G") + "=====" + message);
}
}
private void StopService(string serviceName)
{
using (ServiceController controller =new ServiceController(serviceName))
{
if(controller.Status == ServiceControllerStatus.Running)
{
controller.Stop();
WriteLog(serviceName+" stop");
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 3、添加安装程序
编辑 serviceProcessInstaller1
属性 Accout
为 LocalService
(本地系统)
编辑 serviceInstaller1
# 4、安装服务
# 安装服务
%SystemRoot%\Microsoft.NET\Framework65\v4.0.30319\InstallUtil.exe D:\TestService.exe
# 卸载
%SystemRoot%\Microsoft.NET\Framework65\v4.0.30319\InstallUtil.exe /u D:\TestService.exe
1
2
3
4
5
2
3
4
5
# 5、控制服务
using System;
using System.Collections.Generic;
using System.Text;
namespace StartWindowsServiceDemo
{
class Program
{
static void Main(string[] args)
{
string windowsServiceName = "iphlpsvc";
StartWindowsService(windowsServiceName);
StopWindowsService(windowsServiceName);
Console.Read();
}
/// <summary>
/// 开启服务
/// </summary>
/// <param name="windowsServiceName">服务名称</param>
static void StartWindowsService(string windowsServiceName)
{
using (System.ServiceProcess.ServiceController control = new System.ServiceProcess.ServiceController(windowsServiceName))
{
if (control.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
Console.WriteLine("服务启动......");
control.Start();
Console.WriteLine("服务已经启动......");
}
else if(control.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
Console.WriteLine("服务已经启动......");
}
}
}
/// <summary>
/// 停止服务
/// </summary>
/// <param name="windowsServiceName">服务名称</param>
static void StopWindowsService(string windowsServiceName)
{
using (System.ServiceProcess.ServiceController control = new System.ServiceProcess.ServiceController(windowsServiceName))
{
if (control.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
Console.WriteLine("服务停止......");
control.Stop();
Console.WriteLine("服务已经停止......");
}
else if (control.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
Console.WriteLine("服务已经停止......");
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61