首页 > 代码库 > c#之监控文件结构

c#之监控文件结构

如果需要知道修改文件或目录的时间,可以通过FileSystemWatcher类,这个类提供了一下应用程序可以捕获的事件,应用程序可以对事件作出响应。

使用FileSystemWatcher非常简单,首先必须设置一些属性,指定监控的位置、内容以及引发应用程序要处理事件的时间,然后给FileSystemWatcher提供定制事件处理程序的地址。当事件发生时,FileSystemWatcher就调用这些属性,然后打开FileSystemWatcher,等待事件。

1、在启用FileSystemWatcher对象之前必须设置的属性:

技术分享

 

2、设置了属性之后,必须为4个事件Changed、Created、Deleted、Renamed编写事件处理程序。

3、设置了属性和事件后,将EnableRaisingEvents属性设置为true,就可以开始监控工作了。

 示例:

建立如下窗体:

技术分享

 

 

窗体属性:

技术分享

代码:

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.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        private FileSystemWatcher watcher;
        private delegate void updateTextDEL(string text);
        public Form1()
        {
            InitializeComponent();
            watcher = new FileSystemWatcher();
            watcher.Deleted += watcher_Deleted;
            watcher.Renamed += watcher_Renamed;
            watcher.Changed += watcher_Changed;
            watcher.Created += watcher_Created;
        }
        public void UpdateText(string text)
        {
            lbWatch.Text = text;
        }
        void watcher_Created(object sender, FileSystemEventArgs e)
        {
            StreamWriter sw = new StreamWriter("log.txt", true);
            sw.WriteLine("File:{0} created", e.FullPath);
            sw.Close();
            this.BeginInvoke(new updateTextDEL(UpdateText), "created");
        }

        void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            StreamWriter sw = new StreamWriter("log.txt", true);
            sw.WriteLine("File:{0}{1}", e.FullPath, e.ChangeType, ToString());
            sw.Close();
            this.BeginInvoke(new updateTextDEL(UpdateText), "changed");

        }

        void watcher_Renamed(object sender, RenamedEventArgs e)
        {
            StreamWriter sw = new StreamWriter("log.txt", true);
            sw.WriteLine("File:renamed from{0}to{1}", e.OldName, e.FullPath);
            sw.Close();
            this.BeginInvoke(new updateTextDEL(UpdateText), "renamed");
        }

        void watcher_Deleted(object sender, FileSystemEventArgs e)
        {
            StreamWriter sw = new StreamWriter("log.txt", true);
            sw.WriteLine("File:{0}deleted", e.FullPath);
            sw.Close();
            this.BeginInvoke(new updateTextDEL(UpdateText), "deleted");
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            if(openFileDialog1.ShowDialog()!=DialogResult.Cancel)
            {
                txbLocatin.Text = openFileDialog1.FileName;

            }
        }

        private void btnWatch_Click(object sender, EventArgs e)
        {
            watcher.Path = Path.GetDirectoryName(txbLocatin.Text);
            watcher.Filter = Path.GetFileName(txbLocatin.Text);
            watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters
                .Size;
            lbWatch.Text = "watching" + txbLocatin.Text;
            watcher.EnableRaisingEvents = true;
        }

    }
}

 

c#之监控文件结构