成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C#Windows服務之添加文件監(jiān)視服務

開發(fā) 后端
C#Windows服務中添加文件監(jiān)視服務是如何實現(xiàn)的呢?C#Windows服務中添加文件監(jiān)視服務需要注意什么呢?讓我們帶著這些問題看一下的文章。

C#Windows服務之添加文件監(jiān)視服務:

了解了Windows服務的基本體系結構和創(chuàng)建方法后,我們就可以試著往服務中添加一些實際的功能了。下面我將向大家介紹一個能監(jiān)視本地文件系統(tǒng)的文件監(jiān)視服務-FileMonitorService。該服務能根據(jù)預先設定的本地目錄路徑監(jiān)視其中的文件包括子文件夾中的任何變化:文件創(chuàng)建、文件刪除、文件改名、文件修改。同時,該服務還為每種變化創(chuàng)建了一個相對應的計數(shù)器,計數(shù)器的作用就是反映該種變化的頻度。

首先,我們打開Visual Studio.Net,新建一個Visual C#的Windows服務的項目,如圖所示:

新建C#的Windows服務的項目 

在重載Windows服務的OnStart()函數(shù)之前,我們先給其類添加一些計數(shù)器對象,這些計數(shù)器分別對應了文件的創(chuàng)建、刪除、改名以及修改等變化。一旦指定目錄中的文件發(fā)生以上的某種變化,與其相對應的計數(shù)器就會自動加1。所有的這些計數(shù)器都是定義為PerformanceCounter類型的變量的,該類是包含在System.Diagnostics命名空間中的。

  1. private System.Diagnostics.PerformanceCounter fileCreateCounter;  
  2.  
  3. private System.Diagnostics.PerformanceCounter fileDeleteCounter;  
  4.  
  5. private System.Diagnostics.PerformanceCounter fileRenameCounter;  
  6.  
  7. private System.Diagnostics.PerformanceCounter fileChangeCounter; 

之后我們便在類的InitializeComponent()方法中創(chuàng)建以上定義的各個計數(shù)器對象并確定其相關屬性。同時我們將該Windows服務的名稱設置為“FileMonitorService”,設定其即是允許暫停并恢復的又是允許停止的。

  1. private void InitializeComponent()  
  2.  
  3.  {  
  4.  
  5. this.components = new System.ComponentModel.Container();  
  6.  
  7. this.fileChangeCounter = new System.Diagnostics.PerformanceCounter();  
  8.  
  9. this.fileDeleteCounter = new System.Diagnostics.PerformanceCounter();  
  10.  
  11. this.fileRenameCounter = new System.Diagnostics.PerformanceCounter();  
  12.  
  13. this.fileCreateCounter = new System.Diagnostics.PerformanceCounter();  
  14.  
  15.  
  16. fileChangeCounter.CategoryName = "File Monitor Service";  
  17.  
  18. fileDeleteCounter.CategoryName = "File Monitor Service";  
  19.  
  20. fileRenameCounter.CategoryName = "File Monitor Service";  
  21.  
  22. fileCreateCounter.CategoryName = "File Monitor Service";  
  23.  
  24.    
  25.  
  26. fileChangeCounter.CounterName = "Files Changed";  
  27.  
  28. fileDeleteCounter.CounterName = "Files Deleted";  
  29.  
  30. fileRenameCounter.CounterName = "Files Renamed";  
  31.  
  32. fileCreateCounter.CounterName = "Files Created";  
  33.  
  34.  
  35. this.ServiceName = "FileMonitorService";  
  36.  
  37. this.CanPauseAndContinue = true;  
  38.  
  39. this.CanStop = true;  
  40.  
  41. servicePaused = false;  
  42.  
  43.  } 

接著就是重載OnStart()函數(shù)和OnStop()函數(shù),OnStart()函數(shù)完成了一些必要的初始化工作。在.Net框架下,文件的監(jiān)視功能可以由FileSystemWatcher類來完成,該類是包含在System.IO命名空間下的。該Windows服務所要完成的功能包括了監(jiān)視文件的創(chuàng)建、刪除、改名和修改等變化,而FileSystemWatcher類包含所有了對應于這些變化的處理函數(shù)。

  1. protected override void OnStart(string[] args)  
  2.  
  3.  {       
  4.  
  5. FileSystemWatcher curWatcher = new FileSystemWatcher();  
  6.  
  7.  
  8. curWatcher.BeginInit();  
  9.  
  10. curWatcher.IncludeSubdirectories = true;  
  11.  
  12. curWatcher.Path =  
  13.  
  14. System.Configuration.ConfigurationSettings.AppSettings  
  15.  
  16. ["FileMonitorDirectory"];  
  17.  
  18. curWatcher.Changed += new FileSystemEventHandler(OnFileChanged);  
  19.  
  20. curWatcher.Created += new FileSystemEventHandler(OnFileCreated);  
  21.  
  22. curWatcher.Deleted += new FileSystemEventHandler(OnFileDeleted);  
  23.  
  24. curWatcher.Renamed += new RenamedEventHandler(OnFileRenamed);  
  25.  
  26. curWatcher.EnableRaisingEvents = true;  
  27.  
  28. curWatcher.EndInit();  
  29.  
  30.  } 

注意其中被監(jiān)視的目錄是存放在一個應用程序配置文件中的,該文件是一個XML類型的文件。這種做法的好處就是我們不必重新編譯并發(fā)布該Windows服務而只要直接修改其配置文件就可以達到更改所要監(jiān)視的目錄的功能了。

當該Windows服務啟動后,一旦被監(jiān)視的目錄中的文件發(fā)生某種變化,與其相對應的計數(shù)器的值便會相應的增加,方法很簡單,只要調(diào)用計數(shù)器對象的IncrementBy()即可。

  1. private void OnFileChanged(Object source, FileSystemEventArgs e)  
  2.  
  3.  {  
  4.  
  5. if( servicePaused == false )  
  6.  
  7. {  
  8.  
  9.   fileChangeCounter.IncrementBy(1);  
  10.  
  11. }  
  12.  
  13.  }  
  14.  
  15.  private void OnFileRenamed(Object source, RenamedEventArgs e)  
  16.  
  17.  {  
  18.  
  19. if( servicePaused == false )  
  20.  
  21. {  
  22.  
  23.   fileRenameCounter.IncrementBy(1);  
  24.  
  25. }  
  26.  
  27.  }  
  28.  
  29.    
  30.  
  31.  private void OnFileCreated(Object source, FileSystemEventArgs e)  
  32.  
  33.  {  
  34.  
  35. if( servicePaused == false )  
  36.  
  37. {  
  38.  
  39.   fileCreateCounter.IncrementBy(1);  
  40.  
  41. }  
  42.  
  43.  }  
  44.  
  45.  private void OnFileDeleted(Object source, FileSystemEventArgs e)  
  46.  
  47.  {  
  48.  
  49. if( servicePaused == false )  
  50.  
  51. {  
  52.  
  53.   fileDeleteCounter.IncrementBy(1);  
  54.  
  55. }  
  56.  
  57.  } 

 

OnStop()函數(shù)即是停止Windows服務的,在該Windows服務中,服務一旦停止,所有的計數(shù)器的值都應歸零,但是計數(shù)器并不提供一個Reset()方法,所以我們只好將計數(shù)器中的值減去當前值來達到這個目的。

  1. protected override void OnStop()  
  2.  
  3.  {  
  4.  
  5. if( fileChangeCounter.RawValue != 0 )  
  6.  
  7. {  
  8.  
  9.   fileChangeCounter.IncrementBy(-fileChangeCounter.RawValue);  
  10.  
  11. }  
  12.  
  13. if( fileDeleteCounter.RawValue != 0 )  
  14.  
  15. {  
  16.  
  17.   fileDeleteCounter.IncrementBy(-fileDeleteCounter.RawValue);  
  18.  
  19. }  
  20.  
  21. if( fileRenameCounter.RawValue != 0 )  
  22.  
  23. {  
  24.  
  25.   fileRenameCounter.IncrementBy(-fileRenameCounter.RawValue);        
  26.  
  27. }  
  28.  
  29. if( fileCreateCounter.RawValue != 0 )  
  30.  
  31. {  
  32.  
  33.   fileCreateCounter.IncrementBy(-fileCreateCounter.RawValue);  
  34.  
  35. }  
  36.  
  37.  } 

C#Windows服務中添加文件監(jiān)視服務需要注意的:因為我們的Windows服務是允許暫停并恢復的,所以我們還得重載OnPause()函數(shù)和OnContinue()函數(shù),方法很簡單,只要設定前面定義的布爾值servicePaused即可。

  1. protected override void OnPause()  
  2.  
  3.  {  
  4.  
  5. servicePaused = true;  
  6.  
  7.  }  
  8.  
  9. protected override void OnContinue()  
  10.  
  11.  {  
  12.  
  13. servicePaused = false;  
  14.  

這樣,該Windows服務的主體部分已經(jīng)完成了,不過它并不有用,我們還必須為其添加安裝文件監(jiān)視。安裝文件為Windows服務的正確安裝做好了工作,它包括了一個Windows服務的安裝類,該類是重System.Configuration.Install.Installer繼承過來的。安裝類中包括了Windows服務運行所需的帳號信息,用戶名、密碼信息以及Windows服務的名稱,啟動方式等信息。

  1. [RunInstaller(true)]  
  2.  
  3. public class Installer1 : System.Configuration.Install.Installer  
  4.  
  5.  {  
  6.  
  7.  /// <summary>  
  8.  
  9.  /// 必需的設計器變量。  
  10.  
  11.  /// </summary>  
  12.  
  13.  private System.ComponentModel.Container components = null;  
  14.  
  15. private System.ServiceProcess.ServiceProcessInstaller spInstaller;  
  16.  
  17. private System.ServiceProcess.ServiceInstaller sInstaller;  
  18.  
  19.  public Installer1()  
  20.  
  21.  {  
  22.  
  23. // 該調(diào)用是設計器所必需的。  
  24.  
  25. InitializeComponent();  
  26.  
  27. // TODO: 在 InitComponent 調(diào)用后添加任何初始化  
  28.  
  29.  }  
  30.  
  31.  #region Component Designer generated code  
  32.  
  33.  /// <summary>  
  34.  
  35.  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改  
  36.  
  37.  /// 此方法的內(nèi)容。  
  38.  
  39.  /// </summary>  
  40.  
  41. private void InitializeComponent()  
  42.  
  43.  {  
  44.  
  45. components = new System.ComponentModel.Container();  
  46.  
  47. // 創(chuàng)建ServiceProcessInstaller對象和ServiceInstaller對象  
  48.  
  49. this.spInstaller =   
  50.  
  51. new System.ServiceProcess.ServiceProcessInstaller();  
  52.  
  53. this.sInstaller = new System.ServiceProcess.ServiceInstaller();  
  54.  
  55. // 設定ServiceProcessInstaller對象的帳號、用戶名和密碼等信息  
  56.  
  57. this.spInstaller.Account =   
  58.  
  59. System.ServiceProcess.ServiceAccount.LocalSystem;  
  60.  
  61. this.spInstaller.Username = null;  
  62.  
  63. this.spInstaller.Password = null;  
  64.  
  65.        // 設定服務名稱  
  66.  
  67. this.sInstaller.ServiceName = "FileMonitorService";  
  68.  
  69.    
  70.  
  71.        // 設定服務的啟動方式  
  72.  
  73. this.sInstaller.StartType =   
  74.  
  75. System.ServiceProcess.ServiceStartMode.Automatic;  
  76.  
  77.  
  78. this.Installers.AddRange(  
  79.  
  80. new System.Configuration.Install.Installer[]   
  81.  
  82. {this.spInstaller, this.sInstaller });  
  83.  
  84.  }  
  85.  
  86.  #endregion  
  87.  
  88.        } 

同樣,因為該Windows服務中運用到了計數(shù)器對象,我們也要為其添加相應的安裝文件,安裝文件的內(nèi)容和作用與前面的類似。限于篇幅,這里就不給出相應的代碼了,有興趣的讀者可以參考文后附帶的源代碼文件。

到此為止,整個Windows服務已經(jīng)構建完畢,不過Windows服務程序和一般的應用程序不同,它不能直接調(diào)試運行。如果你直接在IDE下試圖調(diào)試運行之,就會報出如圖所示提示。

Windows服務程序報出提示 

根據(jù)其中提示,我們知道安裝Windows服務需要用到一個名為InstallUtil.exe的命令行工具。而運用該工具安裝Windows服務的方法是非常簡單的,安裝該Windows服務的命令如下:

  1. installutil FileMonitorService.exe 

而要卸載該Windows服務,你只要輸入如下的命令即可:

  1. installutil /u FileMonitorService.exe 

Windows服務安裝成功后,它便會出現(xiàn)在服務控制管理器中,如圖所示。

Windows服務安裝成功 

這樣,該文件監(jiān)視的C#Windows服務就完成了,一旦我們對被監(jiān)視的目錄中的文件進行操作,相應的計數(shù)器就會運作,起到監(jiān)視文件變化的作用。不過這個功能對于一般的用戶而言沒多大意義,然而你可以在此基礎上添加新的功能,比如構建一個后臺的文件處理系統(tǒng),一旦被監(jiān)視的目錄中的文件發(fā)生某種變化,Windows服務便對其進行特定的操作,而最終用戶就不必去關心后臺處理程序是如何實現(xiàn)的了。

C#Windows服務中添加文件監(jiān)視服務的相關內(nèi)容就向你介紹到這里,希望對你學習和了解C#Windows服務中添加文件監(jiān)視服務有所幫助。

【編輯推薦】

  1. C#復制構造函數(shù)的實質淺析
  2. C#允許服務與桌面交互實現(xiàn)淺析
  3. C#windows服務中的Timer控件的使用
  4. C#Windows服務介紹
  5. C#創(chuàng)建Windows服務程序淺析
責任編輯:仲衡 來源: vchome.net
相關推薦

2009-08-14 13:41:13

C#Windows服務

2009-08-14 15:19:38

Windows服務程序Windows服務

2009-08-14 10:50:09

Windows服務介紹

2009-08-14 16:48:39

C#Windows服務

2009-08-14 15:47:18

C#Windows服務

2009-08-14 15:06:08

Windows服務程序

2009-08-14 14:53:55

WINDOWS服務交互

2009-08-14 14:25:09

Windows服務程序

2009-08-14 16:13:25

C#windows服務

2009-08-14 14:45:03

C#Windows服務

2009-08-14 14:17:16

C#Windows服務

2009-08-14 16:24:00

Windows服務程序

2009-08-14 15:54:50

Windows服務程序C#Windows服務

2009-08-14 17:43:20

C#Windows應用

2009-08-14 17:55:52

C#Windows應用

2009-08-14 10:42:16

Timer控件的使用C#windows服務

2009-08-14 17:27:30

C#Windows應用

2009-08-14 17:51:32

C#Windows應用

2009-08-14 18:04:59

C#Windows應用

2009-08-14 17:36:20

C#Windows應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日日操夜夜摸 | 亚洲精品第一 | 亚洲欧美中文日韩在线v日本 | 91成人影院 | 国产99久久精品一区二区永久免费 | 欧美伦理一区 | 国产在线精品一区 | 免费一级黄色 | 国产精品日韩在线观看一区二区 | 午夜理伦三级理论三级在线观看 | 一级毛片在线视频 | 久久99这里只有精品 | 爱爱视频在线观看 | 日本大香伊一区二区三区 | 欧美日韩一区二区在线观看 | 黄色成人在线观看 | 午夜男人免费视频 | 免费av在线 | 日韩在线视频一区 | 亚洲成人免费电影 | 欧美片网站免费 | 久久三区| 一区二区精品电影 | 久久高清免费视频 | 国产精品久久一区二区三区 | 一区二区精品在线 | 亚洲一区二区久久 | 黄色一级电影免费观看 | 一级美国黄色片 | 综合久久久久 | 亚洲精品免费观看 | 中文字幕日韩三级 | 一二区成人影院电影网 | 亚洲三区在线观看 | 亚洲一二三在线 | 日韩精品免费一区 | 免费观看一级毛片视频 | 国产欧美一区二区三区在线看蜜臀 | 久久99蜜桃综合影院免费观看 | 日韩av在线一区 | 黄色一级大片在线免费看产 |