创建Windows服务程序

首先,我们打开Visual Studio.Net,新建一个Visual C#的Windows服务的项目,如图3所示:

  图3



  在重载Windows服务的OnStart()函数之前,我们先给其类添加一些计数器对象,这些计数器分别对应了文件的创建、删除、改名以及修改等变化。一旦指定目录中的文件发生以上的某种变化,与其相对应的计数器就会自动加1。所有的这些计数器都是定义为PerformanceCounter类型的变量的,该类是包含在System.Diagnostics命名空间中的。


private System.Diagnostics.PerformanceCounter fileCreateCounter;

private System.Diagnostics.PerformanceCounter fileDeleteCounter;

private System.Diagnostics.PerformanceCounter fileRenameCounter;

private System.Diagnostics.PerformanceCounter fileChangeCounter;



  之后我们便在类的InitializeComponent()方法中创建以上定义的各个计数器对象并确定其相关属性。同时我们将该Windows服务的名称设置为“FileMonitorService”,设定其即是允许暂停并恢复的又是允许停止的。


private void InitializeComponent()

              {

                     this.components = new System.ComponentModel.Container();

                     this.fileChangeCounter = new System.Diagnostics.PerformanceCounter();

                     this.fileDeleteCounter = new System.Diagnostics.PerformanceCounter();

                      this.fileRenameCounter = new System.Diagnostics.PerformanceCounter();

                     this.fileCreateCounter = new System.Diagnostics.PerformanceCounter();

                     fileChangeCounter.CategoryName = "File Monitor Service";

                      fileDeleteCounter.CategoryName = "File Monitor Service";

                     fileRenameCounter.CategoryName = "File Monitor Service";

                     fileCreateCounter.CategoryName = "File Monitor Service";

                     fileChangeCounter.CounterName = "Files Changed";

                     fileDeleteCounter.CounterName = "Files Deleted";

                     fileRenameCounter.CounterName = "Files Renamed";

                     fileCreateCounter.CounterName = "Files Created";

                      this.ServiceName = "FileMonitorService";

                     this.CanPauseAndContinue = true;

                     this.CanStop = true;

                     servicePaused = false;

              }



  接着就是重载OnStart()函数和OnStop()函数,OnStart()函数完成了一些必要的初始化工作。在.Net框架下,文件的监视功能可以由FileSystemWatcher类来完成,该类是包含在System.IO命名空间下的。该Windows服务所要完成的功能包括了监视文件的创建、删除、改名和修改等变化,而FileSystemWatcher类包含所有了对应于这些变化的处理函数。


protected override void OnStart(string[] args)

              {    

                     FileSystemWatcher curWatcher = new FileSystemWatcher();

                     curWatcher.BeginInit();

                     curWatcher.IncludeSubdirectories = true;

                     curWatcher.Path =

             System.Configuration.ConfigurationSettings.AppSettings

               ["FileMonitorDirectory"];

                     curWatcher.Changed += new FileSystemEventHandler(OnFileChanged);

                     curWatcher.Created += new FileSystemEventHandler(OnFileCreated);

                     curWatcher.Deleted += new FileSystemEventHandler(OnFileDeleted);

                     curWatcher.Renamed += new RenamedEventHandler(OnFileRenamed);

                     curWatcher.EnableRaisingEvents = true;

                     curWatcher.EndInit();

              }



  注意其中被监视的目录是存放在一个应用程序配置文件中的,该文件是一个XML类型的文件。这种做法的好处就是我们不必重新编译并发布该Windows服务而只要直接修改其配置文件就可以达到更改所要监视的目录的功能了。

  当该Windows服务启动后,一旦被监视的目录中的文件发生某种变化,与其相对应的计数器的值便会相应的增加,方法很简单,只要调用计数器对象的IncrementBy()即可。


private void OnFileChanged(Object source, FileSystemEventArgs e)

              {

                     if( servicePaused == false )

                     {

                            fileChangeCounter.IncrementBy(1);

                     }

              }

              private void OnFileRenamed(Object source, RenamedEventArgs e)

              {

                     if( servicePaused == false )

                     {

                            fileRenameCounter.IncrementBy(1);

                     }

              }

              private void OnFileCreated(Object source, FileSystemEventArgs e)

              {

                     if( servicePaused == false )

                     {

                            fileCreateCounter.IncrementBy(1);

                     }

               }

              private void OnFileDeleted(Object source, FileSystemEventArgs e)

              {

                     if( servicePaused == false )

                     {

                            fileDeleteCounter.IncrementBy(1);

                     }

               }



  OnStop()函数即是停止Windows服务的,在该Windows服务中,服务一旦停止,所有的计数器的值都应归零,但是计数器并不提供一个Reset()方法,所以我们只好将计数器中的值减去当前值来达到这个目的。


protected override void OnStop()

              {

                     if( fileChangeCounter.RawValue != 0 )

                     {

                            fileChangeCounter.IncrementBy(-fileChangeCounter.RawValue);

                     }

                     if( fileDeleteCounter.RawValue != 0 )

                     {

                            fileDeleteCounter.IncrementBy(-fileDeleteCounter.RawValue);

                     }

                     if( fileRenameCounter.RawValue != 0 )

                     {

                            fileRenameCounter.IncrementBy(-fileRenameCounter.RawValue);     

                     }

                      if( fileCreateCounter.RawValue != 0 )

                     {

                            fileCreateCounter.IncrementBy(-fileCreateCounter.RawValue);

                     }

              }



  同时,因为我们的Windows服务是允许暂停并恢复的,所以我们还得重载OnPause()函数和OnContinue()函数,方法很简单,只要设定前面定义的布尔值servicePaused即可。


protected override void OnPause()

              {

                     servicePaused = true;

              }

             protected override void OnContinue()

              {

                     servicePaused = false;

             }



  这样,该Windows服务的主体部分已经完成了,不过它并不有用,我们还必须为其添加安装文件。安装文件为Windows服务的正确安装做好了工作,它包括了一个Windows服务的安装类,该类是重System.Configuration.Install.Installer继承过来的。安装类中包括了Windows服务运行所需的帐号信息,用户名、密码信息以及Windows服务的名称,启动方式等信息。


[RunInstaller(true)]

       public class Installer1 : System.Configuration.Install.Installer

       {

              /// <summary>

              /// 必需的设计器变量。

              /// </summary>

              private System.ComponentModel.Container components = null;

              private System.ServiceProcess.ServiceProcessInstaller spInstaller;

              private System.ServiceProcess.ServiceInstaller sInstaller;

              public Installer1()

              {

                     // 该调用是设计器所必需的。

                     InitializeComponent();

                    // TODO: 在 InitComponent 调用后添加任何初始化

              }

              #region Component Designer generated code

              /// <summary>

              /// 设计器支持所需的方法 - 不要使用代码编辑器修改

              /// 此方法的内容。

              /// </summary>

             private void InitializeComponent()

              {

                     components = new System.ComponentModel.Container();

                     // 创建ServiceProcessInstaller对象和ServiceInstaller对象

                     this.spInstaller =

              new System.ServiceProcess.ServiceProcessInstaller();

                     this.sInstaller = new System.ServiceProcess.ServiceInstaller();

                     // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息

                     this.spInstaller.Account =

              System.ServiceProcess.ServiceAccount.LocalSystem;

                     this.spInstaller.Username = null;

                     this.spInstaller.Password = null;

                    // 设定服务名称

                     this.sInstaller.ServiceName = "FileMonitorService";

                    // 设定服务的启动方式

                     this.sInstaller.StartType =

              System.ServiceProcess.ServiceStartMode.Automatic;

                     this.Installers.AddRange(

              new System.Configuration.Install.Installer[]

                {this.spInstaller, this.sInstaller });

              }

              #endregion

       }



  同样,因为该Windows服务中运用到了计数器对象,我们也要为其添加相应的安装文件,安装文件的内容和作用与前面的类似。限于篇幅,这里就不给出相应的代码了,有兴趣的读者可以参考文后附带的源代码文件。

  到此为止,整个Windows服务已经构建完毕,不过Windows服务程序和一般的应用程序不同,它不能直接调试运行。如果你直接在IDE下试图调试运行之,就会报出如图4所示提示。

  图4



  根据其中提示,我们知道安装Windows服务需要用到一个名为InstallUtil.exe的命令行工具。而运用该工具安装Windows服务的方法是非常简单的,安装该Windows服务的命令如下:


installutil FileMonitorService.exe



  而要卸载该Windows服务,你只要输入如下的命令即可:


installutil /u FileMonitorService.exe



  Windows服务安装成功后,它便会出现在服务控制管理器中,如图5所示。

  图5



  这样,该文件监视的Windows服务就完成了,一旦我们对被监视的目录中的文件进行操作,相应的计数器就会运作,起到监视文件变化的作用。不过这个功能对于一般的用户而言没多大意义,然而你可以在此基础上添加新的功能,比如构建一个后台的文件处理系统,一旦被监视的目录中的文件发生某种变化,Windows服务便对其进行特定的操作,而最终用户就不必去关心后台处理程序是如何实现的了。

原文地址:https://www.cnblogs.com/Magicam/p/1493044.html