码迷,mamicode.com
首页 > Windows程序 > 详细

c#之监控文件结构

时间:2017-04-26 10:03:22      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:change   ogre   ini   txt   dial   位置   rms   调用   通过   

如果需要知道修改文件或目录的时间,可以通过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#之监控文件结构

标签:change   ogre   ini   txt   dial   位置   rms   调用   通过   

原文地址:http://www.cnblogs.com/lfxiao/p/6766795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!