使用 FileSystemWatcher 監(jiān)視指定目錄中的更改??杀O(jiān)視指定目錄中的文件或子目錄的更改。該組件可以監(jiān)視本地計(jì)算機(jī)、網(wǎng)絡(luò)驅(qū)動(dòng)器或遠(yuǎn)程計(jì)算機(jī)上的文件。
可監(jiān)視目錄或文件中的若干種更改。例如,可監(jiān)視文件或目錄的 Attributes、LastWrite 日期和時(shí)間或 Size 方面的更改。通過(guò)設(shè)置FileSystemWatcher.NotifyFilter屬性。
可監(jiān)視文件或目錄的重命名、刪除或創(chuàng)建。例如,若要監(jiān)視文本文件的重命名,請(qǐng)將 Filter 屬性設(shè)置為“*.txt”。注意 公共文件系統(tǒng)操作可能會(huì)引發(fā)多個(gè)事件。例如,將文件從一個(gè)目錄移到另一個(gè)目錄時(shí),可能會(huì)引發(fā)若干 OnChanged 以及一些 OnCreated 和 OnDelete事件。移動(dòng)文件是一個(gè)包含多個(gè)簡(jiǎn)單操作的復(fù)雜操作,因此會(huì)引發(fā)多個(gè)事件。同樣,一些應(yīng)用程序(如反病毒軟件)可能導(dǎo)致被 FileSystemWatcher 檢測(cè)到的附加文件系統(tǒng)事件。
注意FileSystemWatcher.NotifyFilter 屬性設(shè)置的監(jiān)視和事件(OnChanged,OnCreated,OnDelete等事件)是and的關(guān)系。
例子程序:
public class Watcher { public static void Main() { string[] args = System.Environment.GetCommandLineArgs(); // If a directory is not specified, exit program. if(args.Length != 2) { // Display the proper way to call the program. Console.WriteLine("Usage: Watcher.exe (directory)"); return; } // Create a new FileSystemWatcher and set its properties. FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = args[1]; /* Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. */ watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // Only watch text files. watcher.Filter = "*.txt"; // Add event handlers. watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); watcher.Deleted += new FileSystemEventHandler(OnChanged); watcher.Renamed += new RenamedEventHandler(OnRenamed); // Begin watching. watcher.EnableRaisingEvents = true; //Suspend the calling thread until the file has been created WaitForChangedResult cr= watcher.WaitForChanged(WatcherChangeTypes.Created); // Wait for the user to quit the program. Console.WriteLine("Press /'q/' to quit the sample."); while(Console.Read()!='q'); } // Define the event handlers. private static void OnChanged(object source, FileSystemEventArgs e) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); } private static void OnRenamed(object source, RenamedEventArgs e) { // Specify what is done when a file is renamed. Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath); } }
但是,在實(shí)際使用過(guò)程中,由于需要監(jiān)視的文件,必須在復(fù)制完成后才能進(jìn)行后續(xù)工作,所以在監(jiān)視時(shí),需要不斷進(jìn)行判斷,判斷文件是否復(fù)制完成。所以修改
private static void OnChanged(object source, FileSystemEventArgs e) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + " " + e.ChangeType); string filepath = e.FullPath; //mypath + "//" + cr.Name; FileInfo fi = new FileInfo(filepath); if (!fi.Exists) { Console.WriteLine("file not exits!!"); } Console.WriteLine(fi.IsReadOnly.ToString()); //if (!fi.IsReadOnly) //{ // fi.AppendText(); //} HELLO: try { fi.OpenRead(); } catch (IOException ex) { Console.WriteLine(ex.Message); Thread.Sleep(3000); goto HELLO; } OnRMChanged(e.FullPath);//對(duì)文件進(jìn)行任意處理 }
最后等復(fù)制完成再對(duì)文件進(jìn)行操作如下
private static void OnRMChanged(string mypath) { //對(duì)文件進(jìn)行任意處理 }
根據(jù)該文章改造成我自己的文件監(jiān)控器,哈哈
FileSystemWatcher監(jiān)聽(tīng)文件是否有被修改
作用:監(jiān)聽(tīng)文件系統(tǒng)更改通知,并在目錄或目錄中的文件更改時(shí)引發(fā)事件。
需求:監(jiān)聽(tīng)特定文件是否修改,然后做出相應(yīng)的操作。
方法:
①利用一個(gè)線程,一直去查找該指定的文件是否有被修改,如果修改則操作特定步驟,否則繼續(xù)查詢。
缺點(diǎn):占用CPU,要一直循環(huán)查找。
②利用.net里面的FileSystemWatcher來(lái)監(jiān)聽(tīng)文件是否有被修改,如果有,則操作特定步驟。
代碼:
①定義一個(gè)全局變量Watch
FileSystemWatcher Watch;
②初始化該全局變量
Watch = new FileSystemWatcher(); Watch.Path = @"C:\Users\RAPOO\Desktop\123"; Watch.Filter = "modlist.mod"; Watch.NotifyFilter = NotifyFilters.LastWrite; Watch.IncludeSubdirectories = false; Watch.Changed += new FileSystemEventHandler(watch_changed); Watch.EnableRaisingEvents = true;
③相應(yīng)修改事件
private void watch_changed(object source, FileSystemEventArgs e) { if (Watch != null) { try { Watch.EnableRaisingEvents = false; MessageBox.Show("改變?。?!"); } finally { Watch.EnableRaisingEvents = true; } } }
注意:
1、代碼只添加修改事件,還有重命名、刪除、新增事件。
2、在修改事件里面,需要將事件監(jiān)控先重置為false,待執(zhí)行結(jié)束后再修改為true。目的是解決修改事件執(zhí)行兩次的BUG。
下圖為MSDN上注釋。
如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答??! 點(diǎn)擊進(jìn)入論壇