快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

C#怎么编写Windows服务程序-创新互联

小编给大家分享一下C#怎么编写Windows服务程序,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

目前创新互联建站已为数千家的企业提供了网站建设、域名、网络空间、网站托管、服务器租用、企业网站设计、烈山网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

一、创建一个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服务程序

看完了这篇文章,相信你对“C#怎么编写Windows服务程序”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道,感谢各位的阅读!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:C#怎么编写Windows服务程序-创新互联
当前路径:http://6mz.cn/article/ddccch.html

其他资讯