C# FileSystemWatcher監聽文件事件


現有一個需求如下:監控某個目錄中的文件修改,創建,刪除等信息,並記錄下來.

這里用到FileSystemWatcher類.由於考慮到文件的寫入量會很頻率,所以考慮先將監聽到的消息記錄到內存中。

 

監聽部分的代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileEventListener
{
    public partial class FSWControl : Form
    {
        static int count = 0;
        static FileSystemWatcher watcher = new FileSystemWatcher();
        static int eventCount = 0;

        //static List<string> files = new List<string>();
        public FSWControl()
        {
            InitializeComponent();
            WatcherStrat(@"D:\test\", "*.tr", true, true);
        }


        /// <summary>
        /// 初始化監聽
        /// </summary>
        /// <param name="StrWarcherPath">需要監聽的目錄</param>
        /// <param name="FilterType">需要監聽的文件類型(篩選器字符串)</param>
        /// <param name="IsEnableRaising">是否啟用監聽</param>
        /// <param name="IsInclude">是否監聽子目錄</param>
        private static void WatcherStrat(string StrWarcherPath, string FilterType, bool IsEnableRaising, bool IsInclude)
        {
            //初始化監聽
            watcher.BeginInit();
            //設置監聽文件類型
            watcher.Filter = FilterType;
            //設置是否監聽子目錄
            watcher.IncludeSubdirectories = IsInclude;
            //設置是否啟用監聽?
            watcher.EnableRaisingEvents = IsEnableRaising;
            //設置需要監聽的更改類型(如:文件或者文件夾的屬性,文件或者文件夾的創建時間;NotifyFilters枚舉的內容)
            watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
            //設置監聽的路徑
            watcher.Path = StrWarcherPath;
            //注冊創建文件或目錄時的監聽事件
            //watcher.Created += new FileSystemEventHandler(watch_created);
            //注冊當指定目錄的文件或者目錄發生改變的時候的監聽事件
            watcher.Changed += new FileSystemEventHandler(watch_changed);
            //注冊當刪除目錄的文件或者目錄的時候的監聽事件
            watcher.Deleted += new FileSystemEventHandler(watch_deleted);
            //當指定目錄的文件或者目錄發生重命名的時候的監聽事件
            watcher.Renamed += new RenamedEventHandler(watch_renamed);
            //結束初始化
            watcher.EndInit();
        }


        /// <summary>
        /// 創建文件或者目錄時的監聽事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void watch_created(object sender, FileSystemEventArgs e)
        {
            //事件內容
            output("create:" + e.FullPath);
        }

        /// <summary>
        /// 當指定目錄的文件或者目錄發生改變的時候的監聽事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void watch_changed(object sender, FileSystemEventArgs e)
        {
            //事件內容
            //事件內容
            output("change:" + e.FullPath);
        }
        /// <summary>
        /// 當刪除目錄的文件或者目錄的時候的監聽事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void watch_deleted(object sender, FileSystemEventArgs e)
        {
            //事件內容
            output("del:" + e.FullPath);
        }
        /// <summary>
        /// 當指定目錄的文件或者目錄發生重命名的時候的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void watch_renamed(object sender, RenamedEventArgs e)
        {
            //事件內容
            output("rename:" + e.FullPath);
        }
        /// <summary>
        /// 啟動或者停止監聽
        /// </summary>
        /// <param name="IsEnableRaising">True:啟用監聽,False:關閉監聽</param>
        private void WatchStartOrSopt(bool IsEnableRaising)
        {
            watcher.EnableRaisingEvents = IsEnableRaising;
        }


        private static void output(string text)
        {
            //FileStream fs = new FileStream("D:\\listen.txt", FileMode.Append);
            //StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            //sw.WriteLine(text);
            //sw.Close();
            //fs.Close();
            //files.Add(text);
            eventCount++;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = count.ToString() + "_" + eventCount.ToString();
            FileStream fs = new FileStream("D:\\listen_" + name + ".txt", FileMode.Append);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            //foreach (string text in files)
            //{
            //    sw.WriteLine(text);
            //}
            sw.WriteLine("共收到事件:" + eventCount.ToString());
            //files.Clear();
            eventCount = 0;
            sw.Close();
            fs.Close();
            count++;
            MessageBox.Show("打印完成:" + name, "提示", MessageBoxButtons.OK);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WatchStartOrSopt(false);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            WatchStartOrSopt(true);
        }
    }
}

 

然后寫一個生成文件的程序用於測試,由於可能需要多個寫入一起跑,采用傳入參數的方式進行調用程序。共三個參數:第一個為標識,第二個為生成文件的數量,第三個為開始運行的時間:

如 FileCreater.exe 1 1000 121000 則表示這個實例的名稱為1,連續生成1000個文件,在12:10:00開始執行。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileCreater
{
    public partial class Form1 : Form
    {
        string filePre = "";
        string startTime = "";

        public Form1(string[] args)
        {
            filePre = args[0];
            startTime = args[2];
            InitializeComponent();

            timer1.Interval = 100;
            timer1.Tick += button1_Click;
            timer1.Enabled = true;
            this.Text = "FileCreater" + filePre + "_" + startTime;
            this.numericUpDown1.Value = int.Parse(args[1]);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label3.Text = DateTime.Now.ToString("HHmmss") + "|" + startTime;
            if (int.Parse(DateTime.Now.ToString("HHmmss")) > int.Parse(startTime))
            {
                label3.Text = numericUpDown1.Value.ToString();
                for (int i = 1; i <= numericUpDown1.Value; i++)
                {
                    //string time = DateTime.Now.ToString("ddhhMMss");
                    string file = string.Format(@"{0}\{1}_{2}.tr", textBox1.Text, i, filePre);
                    FileStream fs = new FileStream(file, FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                    sw.WriteLine(i.ToString());
                    sw.Close();
                    fs.Close();
                }
                timer1.Stop();
            }
            //MessageBox.Show("已完成", "提示", MessageBoxButtons.OK);

            Application.Exit();
        }
    }
}

 

測試過程中發現如下現象:

1.一個生成文件程序,連續生成10W的文件時會發生計數器記錄下的值小於或多於10W


免責聲明!

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



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