leo.net
学无止境,追求完美!

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
namespace WindowsService_TestV0._1
{
 public class Service1 : System.ServiceProcess.ServiceBase
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;
  private System.Diagnostics.PerformanceCounter fileCreateCounter;
  private System.Diagnostics.PerformanceCounter fileDeleteCounter;
  private System.Diagnostics.PerformanceCounter fileRenameCounter;
  private System.Diagnostics.PerformanceCounter fileChangeCounter;
  private bool servicePaused = false;
 


  public Service1()
  {
   // 该调用是 Windows.Forms 组件设计器所必需的。
   InitializeComponent();

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

  // 进程的主入口点
  static void Main()
  {
   System.ServiceProcess.ServiceBase[] ServicesToRun;
 
   // 同一进程中可以运行多个用户服务。若要将
   //另一个服务添加到此进程,请更改下行
   // 以创建另一个服务对象。例如,
   //
   //   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
   //
   ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

   System.ServiceProcess.ServiceBase.Run(ServicesToRun);

 
  }

  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  /// </summary>
  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;
   

  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  /// <summary>
  /// 设置具体的操作,以便服务可以执行它的工作。
  /// </summary>
  protected override void OnStart(string[] args)
  {
   // TODO: 在此处添加代码以启动服务。
   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();

  }
 
  /// <summary>
  /// 停止此服务。
  /// </summary>
  protected override void OnStop()
  {
   // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
   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);
   }

  }

  protected override void OnPause()
  {
   servicePaused = true;
  }

  protected override void OnContinue()
  {
   servicePaused = false;
  }
 

  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);
   }
  }
  
 }
}

posted on 2005-08-20 13:46  LEO.NET  阅读(2074)  评论(4编辑  收藏  举报