全面剖析 System.Diagnostics; System.ServiceProcess;

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

转载于:https://www.cnblogs.com/leoajun/archive/2005/08/20/219059.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值