在这里,我将在c# . net中解释Windows服务。
1、Windows服务的介绍。
2、如何在c# . net中创建Windows服务。
Windows服务通常在操作系统OS启动并在后台运行应用程序时启动。Windows服务在自己的会话中执行应用程序。它可以自动启动,也可以手动暂停、停止和重新启动。
您可以通过以下方式找到服务:
1、进入控制面板选择“服务”内部的“管理工具”。
2、打开命令行窗口(窗口+ R),输入service.msc,按回车。
步骤1
打开Visual Studio,打开>文件新建和选择项目。现在从对话框中选择一个新项目,选择“Window Service”并单击OK按钮。
步骤2
进入Visual c# ->“Windows Desktop””->“Windows Service”,给你的项目一个合适的名字,然后点击OK
一旦你点击OK,下面的屏幕就会出现,这就是你的服务
步骤3
右键单击空白区域并选择“添加安装程序”。
在运行Windows服务之前,需要安装安装程序,安装程序将它注册到服务控制管理器。
添加安装程序之后,ProjectInstaller将添加到项目和ProjectInstakker.cs文件将被打开。别忘了(按ctrl + shift + s键)保存项目。
解决方案浏览器如下图:
步骤4
右键单击空白区域,选择“查看代码”
步骤5
它有一个构造函数,它包含InitializeComponent方法。
InitializeComponent方法包含创建和初始化用户界面对象的逻辑,这些对象拖在表单的表面上,并提供表单设计器的属性网格。
非常重要:在调用InitializeComponent方法之前,不要尝试调用任何方法。
步骤6
选择InitializeComponent方法并按F12键进入定义。
步骤7
现在在安装服务时添加以下一行:
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
您还可以添加描述并显示服务名称(可选)。
this.serviceInstaller1.Description = "My First Service demo";
this.serviceInstaller1.DisplayName = "MyFirstService.Demo";
步骤8
在这一步中,我们将实现一个计时器,并编写代码在给定的时间调用服务。我们将在文本文件中创建一个简单的写操作。
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 MyFirstService {
public partial class Service1: ServiceBase {
Timer timer = new Timer(); // name space(using System.Timers;)
public Service1() {
InitializeComponent();
}
protected override void OnStart(string[] args) {
WriteToFile("Service is started at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 5000; //number in milisecinds
timer.Enabled = true;
}
protected override void OnStop() {
WriteToFile("Service is stopped at " + DateTime.Now);
}
private void OnElapsedTime(object source, ElapsedEventArgs e) {
WriteToFile("Service is recall at " + DateTime.Now);
}
public void WriteToFile(string Message) {
string path = AppDomain.CurrentDomain.BaseDirectory + "\Logs";
if (!Directory.Exists(path)) {
Directory.CreateDirectory(path);
}
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\Logs\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath)) {
// Create a file to write to.
using(StreamWriter sw = File.CreateText(filepath)) {
sw.WriteLine(Message);
}
} else {
using(StreamWriter sw = File.AppendText(filepath)) {
sw.WriteLine(Message);
}
}
}
}
}
代码说明——上面的代码将每5秒调用一个服务,如果不存在,则创建一个文件夹并写我们的消息。
右键单击您的项目或解决方案并选择Rebuild。
第十步
以管理员运行的方式打开命令行:
步骤11
在命令提示符中启动下面的命令并按Enter。
cd C:WindowsMicrosoft.NETFrameworkv4.0.30319
现在进入的项目源文件目录> bin > Debug ,然后复制Windows服务exe文件的完整路径
步骤13
打开命令提示符并启动下面的命令并按enter键。
InstallUtil.exe + Your copied path + your service name + .exe
InstallUtil.exe C:UsersFaisal-PathansourcereposMyFirstServiceMyFirstServicebinDebugMyFirstService.exe
步骤14
按以下步骤打开服务:
按下窗口键+ R,打开命令行窗口。
输入services.msc
找到你的服务。
将在您的bin文件夹中创建一个日志文件夹。
如果您想卸载服务,请启动下面的命令。
InstallUtil.exe -u +复制路径+服务名+ .exe
我们的路径 InstallUtil.exe -u C:UsersFaisal-PathansourcereposMyFirstServiceMyFirstServicebinDebugMyFirstService.exe
在本文中,我们学习了如何使用来自命令行的InstallUtil.exe创建Windows服务并安装/卸载它。
我希望我已经清楚地解释了每个步骤,让所有开发人员能轻松理解。你可以给这篇文章留下反馈/评论/问题。