C#監控文件發生變化


轉自:C# 實現監控文件夾和里面文件的變化 - 秋天的林子 - 博客園 (cnblogs.com)

描述:

  • FileSystemWatcher位於System.IO;
  • 命名空間下面,想要使用它必須先引用System.IO;

下面的例子使用了FileSystemWatcher來監控C盤文件的變化,因為這個目錄的變化是最頻繁的,運行程序打開瀏覽器可以看到很頻繁的臨時文件創建,需要注意的是:

  • 添加文件或文件夾時,會觸發Created事件,然后修改默認文件夾或文件名稱再觸發Changed事件。
  • 復制或移動文件夾文件時則是觸發Created事件。
  • 刪除文件夾或文件時觸發Deleted事件。

下面是整個例子的代碼,可以建一個空的控制台應用程序復制粘貼就能運行了:

 

復制代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace HelloCsharp
{
    class Program
    {
        static void Main(string[] args)
        {
            FileListenerServer f1 = new FileListenerServer(@"c:\");
            f1.Start();
            Console.ReadKey();
        }
    }
    public class FileListenerServer
    {
        private FileSystemWatcher _watcher;
        public FileListenerServer()
        {
        }
        public FileListenerServer(string path)
        {
 
            try
            {
 
                this._watcher = new FileSystemWatcher();
                _watcher.Path = path;
                _watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.DirectoryName;
                _watcher.IncludeSubdirectories = true;
                _watcher.Created += new FileSystemEventHandler(FileWatcher_Created);
                _watcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);
                _watcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);
                _watcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);
 
            }
 
            catch (Exception ex)
            {
                Console.WriteLine("Error:" + ex.Message);
            }
        }
 
 
        public void Start()
        {
 
            this._watcher.EnableRaisingEvents = true;
            Console.WriteLine("文件監控已經啟動...");
 
        }
 
        public void Stop()
        {
 
            this._watcher.EnableRaisingEvents = false;
            this._watcher.Dispose();
            this._watcher = null;
 
        }
 
        protected void FileWatcher_Created(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("新增:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
 
        }
        protected void FileWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("變更:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
        }
        protected void FileWatcher_Deleted(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("刪除:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
        }
        protected void FileWatcher_Renamed(object sender, RenamedEventArgs e)
        {
 
            Console.WriteLine("重命名: OldPath:{0} NewPath:{1} OldFileName{2} NewFileName:{3}", e.OldFullPath, e.FullPath, e.OldName, e.Name);
 
        }
 
    }
}


 
復制代碼

 更改監聽配置信息:

 1   private static void WatcherStrat(string StrWarcherPath, string FilterType, bool IsEnableRaising, bool IsInclude)
 2         {
 3             //初始化監聽
 4             watcher.BeginInit();
 5             //設置監聽文件類型
 6             watcher.Filter = FilterType;
 7             //設置是否監聽子目錄
 8             watcher.IncludeSubdirectories = IsInclude;
 9             //設置是否啟用監聽?
10             watcher.EnableRaisingEvents = IsEnableRaising;
11             //設置需要監聽的更改類型(如:文件或者文件夾的屬性,文件或者文件夾的創建時間;NotifyFilters枚舉的內容)
12             watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
13             //設置監聽的路徑
14             watcher.Path = StrWarcherPath;
15             //注冊創建文件或目錄時的監聽事件
16             //watcher.Created += new FileSystemEventHandler(watch_created);
17             //注冊當指定目錄的文件或者目錄發生改變的時候的監聽事件
18             watcher.Changed += new FileSystemEventHandler(watch_changed);
19             //注冊當刪除目錄的文件或者目錄的時候的監聽事件
20             watcher.Deleted += new FileSystemEventHandler(watch_deleted);
21             //當指定目錄的文件或者目錄發生重命名的時候的監聽事件
22             watcher.Renamed += new RenamedEventHandler(watch_renamed);
23             //結束初始化
24             watcher.EndInit();
25         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM