It would probably render the application more complex
blog里所做读书笔记引用的原文内容版权都归原书作者所有,如果侵权,请告知,谢谢。
blog里所翻译文章,原文版权以及程序代码的版权都为原作者所有。
联系方式:
protected override void OnStart(string[] args) { } protected override void OnStop() { } protected override void OnPause() { } protected override void OnContinue() { }
{ System.ServiceProcess.ServiceBase[] MyServices; MyServices = new System.ServiceProcess.ServiceBase[] { new Service1(), new Service2() }; System.ServiceProcess.ServiceBase.Run(MyServices); }
private System.Diagnostics.PerformanceCounter fileCreateCounter; private System.Diagnostics.PerformanceCounter fileDeleteCounter; private System.Diagnostics.PerformanceCounter fileRenameCounter; private System.Diagnostics.PerformanceCounter fileChangeCounter;
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; }
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(); }
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); } }
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); } }
protected override void OnPause() { servicePaused = true; } protected override void OnContinue() { servicePaused = false; }
[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 }
installutil FileMonitorService.exe
installutil /u FileMonitorService.exe
Powered by: 博客园 Copyright © Ansel