欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#编写Windows服务程序详细步骤详解(图文)

程序员文章站 2023-12-17 09:12:52
一、创建一个windows service 1)创建windows service项目   2)对service重命名 将se...

一、创建一个windows service

1)创建windows service项目

C#编写Windows服务程序详细步骤详解(图文)

C#编写Windows服务程序详细步骤详解(图文) 

2)对service重命名

将service1重命名为你服务名称,这里我们命名为servicetest。

二、创建服务安装程序

1)添加安装程序

C#编写Windows服务程序详细步骤详解(图文) 

C#编写Windows服务程序详细步骤详解(图文) 

之后我们可以看到上图,自动为我们创建了projectinstaller.cs以及2个安装的组件。

2)修改安装服务名

右键serviceinsraller1,选择属性,将servicename的值改为servicetest。

C#编写Windows服务程序详细步骤详解(图文) 

3)修改安装权限

右键serviceprocessinsraller1,选择属性,将account的值改为localsystem。

C#编写Windows服务程序详细步骤详解(图文) 

三、写入服务代码

1)打开servicetest代码

右键servicetest,选择查看代码。

2)写入service逻辑

添加如下代码:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.diagnostics;
using system.linq;
using system.serviceprocess;
using system.text;
namespace windowsservicetest 
{
	public partial class servicetest : servicebase 
	{
		public servicetest() 
		{
			initializecomponent();
		}
		protected override void onstart(string[] args) 
		{
			using (system.io.streamwriter sw = new system.io.streamwriter("c:\\log.txt", true)) 
			{
				sw.writeline(datetime.now.tostring("yyyy-mm-dd hh:mm:ss ") + "start.");
			}
		}
		protected override void onstop() 
		{
			using (system.io.streamwriter sw = new system.io.streamwriter("c:\\log.txt", true)) 
			{
				sw.writeline(datetime.now.tostring("yyyy-mm-dd hh:mm:ss ") + "stop.");
			}
		}
	}
}

这里我们的逻辑很简单,启动服务的时候写个日志,关闭的时候再写个日志。

四、创建安装脚本

在项目中添加2个文件如下(必须是ansi或者utf-8无bom格式):

1)安装脚本install.bat

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe windowsservicetest.exe

net start servicetest

sc config servicetest start= auto

2)卸载脚本uninstall.bat

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe /u windowsservicetest.exe

3)安装脚本说明

第二行为启动服务。

第三行为设置服务为自动运行。

这2行视服务形式自行选择。

4)脚本调试

如果需要查看脚本运行状况,在脚本最后一行加入pause

五、在c#中对服务进行控制

0)配置目录结构

简历一个新wpf项目,叫windowsservicetestui,添加对system.serviceprocess的引用。

在windowsservicetestui的bin\debug目录下建立service目录。

将windowsservicetest的生成目录设置为上面创建的service目录。

生成后目录结构如下图

C#编写Windows服务程序详细步骤详解(图文) 

1)安装

安装时会产生目录问题,所以安装代码如下:

string currentdirectory = system.environment.currentdirectory;
system.environment.currentdirectory = currentdirectory + "\\service";
process process = new process();
process.startinfo.useshellexecute = false;
process.startinfo.filename = "install.bat";
process.startinfo.createnowindow = true;
process.start();
system.environment.currentdirectory = currentdirectory;

2)卸载

卸载时也会产生目录问题,所以卸载代码如下:

string currentdirectory = system.environment.currentdirectory;

system.environment.currentdirectory = currentdirectory + "\\service";
process process = new process();
process.startinfo.useshellexecute = false;
process.startinfo.filename = "uninstall.bat";
process.startinfo.createnowindow = true;
process.start();
system.environment.currentdirectory = currentdirectory;

3)启动

代码如下:

using system.serviceprocess; 
servicecontroller servicecontroller = new servicecontroller("servicetest");
servicecontroller.start();

4)停止

servicecontroller servicecontroller = new servicecontroller("servicetest");
if (servicecontroller.canstop)
servicecontroller.stop();

5)暂停/继续

servicecontroller servicecontroller = new servicecontroller("servicetest");
if (servicecontroller.canpauseandcontinue){
if (servicecontroller.status == servicecontrollerstatus.running)
servicecontroller.pause();
else if (servicecontroller.status == servicecontrollerstatus.paused)
servicecontroller.continue();
}

6)检查状态

servicecontroller servicecontroller = new servicecontroller("servicetest");

string status = servicecontroller.status.tostring();

六、调试windows service

1)安装并运行服务

2)附加进程

C#编写Windows服务程序详细步骤详解(图文)C#编写Windows服务程序详细步骤详解(图文) 

3)在代码中加入断点进行调试

C#编写Windows服务程序详细步骤详解(图文) 

七、总结

本文对windows service的上述配置都未做详细解释,但是按上述步骤就可以制作可运行的windows service,从而达到了工作的需求。

上一篇:

下一篇: