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