WindowsService 使用C#建立简单的Windows服务
大家有用过Windows系统都应该知道,在cmd运行services.msc后出现一个Windows服务的窗体,里面有很多服务,里面可以找到数据库服务,ASP.NET状态服务(进程外存放Session的);

下面我们就手动来创建一个属于自己的Windows 服务:
我们先来说说Main()函数中的代码:
using System; using System.Collections.Generic; using System.Linq; using System.ServiceProcess; using System.Text; namespace MyWindowsServiceDemo { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); } } }
里面我们看到讲一个ServiceBase的数组传递给该类的一个静态方法,ServicesToRun数据存放的对象就是该Windows服务的类;Service1就是Windows服务运行的类;
该类中有两个方法。分别是一个OnStart方法和易个OnStop方法,一个是在服务启动的时候执行的方法,一个是在服务停止的是执行的方法;代码如下:
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 MyWindowsServiceDemo
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
}
我们在服务启动和停止的时候分别在磁盘上写一个文件;
分别在Services1类中的OnStart和OnStop方法添加如下代码:
protected override void OnStart(string[] args)
{
File.WriteAllText(@"E:\OnStart.txt", DateTime.Now.ToString() + "服务启动成功~~~~");
}
protected override void OnStop()
{
File.WriteAllText(@"E:\OnStop.txt", DateTime.Now.ToString() + "服务停止成功~~~~");
}
我们把代码写好了过后,我还要添加服务的安装类:
步骤:切换到Service1.cs文件的视图模型;
右键单击后可以单击添加安装程序,此时会在你项目中自动添加一个ProjectInstaller.cs名字的安装文件,你会惊奇的发现它也有视图模型和后台代码,该后台代码是分部类由partial修士的类,Wdonws服务安装类需要继承制System.Configuration.Install.Installer这个类;此时可以安装该服务了

此时我们需要设置服务的服务名,服务描述和服务进程等,代码如下(还望老鸟指教使用
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem.User;如何安装服务)
):
namespace MyWindowsServiceDemo
{
partial class ProjectInstaller
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "ServiceName";
this.serviceInstaller1.Description = "服务说明~~~";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
#endregion
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
}
}
我们以管理员身份运行VS的命令提示符或者cmd命令切换到您Windows服务的Bin目录下运行安装服务,命令如下:
installutil MyWindowsServiceDemo.exe --服务安装命令
installutil /u MyWindowsServiceDemo.exe --服务卸载命令
安装完成后在运行cmd中运行services.msc出现服务的时候就可以看到我们自己写的服务运行了,此时看磁盘下就会存在刚刚服务启动的文件存在,停止服务后就会有服务停止时写好的文件,效果如下图:

服务启动成功后磁盘上的文件:

服务停止后磁盘上的文件:

最后还望各位指教指教,使用服务安装进程的Account设置为User的枚举的时候怎么设置在某个用户下运行,小弟么有实现,用户名和密码如何设置?

浙公网安备 33010602011771号