创建一个Windows Service 程序
1.新建Windows项目,选择"Windows服务"类型的项目。
2.在生成的Service1.cs中代码中写你需要的代码,如下:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.IO;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
Timer timer;
public Service1() { InitializeComponent(); }
protected override void OnStart(string[] args)
{
timer = new Timer(1000);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
protected override void OnStop()
{
timer.Stop();
timer.Dispose();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
string filePath = AppDomain.CurrentDomain.BaseDirectory + "test.txt";
StreamWriter sw = null;
if (!File.Exists(filePath))
{
sw = File.CreateText(filePath);
}
else
{
sw = File.AppendText(filePath);
}
sw.Write("访问时间:" + DateTime.Now.ToString() + Environment.NewLine);
sw.Close();
}
}
}
3.在和Service1.cs这个项目下在新建一个安装程序类Installer1.cs,代码如下:
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
namespace WindowsService1
{
[RunInstaller(true)]
public partial class Installer1 : Installer
{
private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;
public Installer1()
{
InitializeComponent();
// 创建ServiceProcessInstaller对象和ServiceInstaller对象
this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();
this.sInstaller = new System.ServiceProcess.ServiceInstaller();
// 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息
this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.spInstaller.Password = null;
this.spInstaller.Username = null;
// 设定服务的名称
this.sInstaller.ServiceName = "MyTestWindowsService1";
//设定服务启动的方式
this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller, this.sInstaller });
}
}
}
4.生成工程,在bin目录下会生成exe文件。如果直接运行exe文件的话,是不能执行的,需要使用安装Windows服务用到一个名为InstallUtil.exe的命令行工具,打开命令行工具,转到InstallUtil.exe的目录下,对应的目录为:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe,然后执行InstallUtil.exe+待执行的exe文件的目录,如:InstallUtil.exe F:\MyProject\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe。执行成功后,会在Windows的服务中,出现了刚刚添加的服务的名称。
打开Windows服务列表方式:运行 --> 输入services.msc
在列表中查找一个叫MyTestWindowsService1 的服务,这个就是你创建安装的服务
5.启动该服务,这时打开bin\Debug文件夹,发现已经生成了一个test.txt的文件,里面记录了时间。这说明服务已经正式开始执行。
6.卸载服务的操作也和简单,打开命令行工具,转到C:\Windows\Microsoft.NET\Framework\v4.0.30319目录,然后执行InstallUtil.exe -u F:\MyProject\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe命令就可以了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述