首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用FileWatcher实现文件实时监视

利用FileWatcher实现文件实时监视

作者头像
菩提树下的杨过
发布2018-01-23 10:40:39
1.3K0
发布2018-01-23 10:40:39
举报

FileWatcher能实现对某一目录的文件(新建,改名,内容修改,删除)的实时监视

using System;
using System.IO;
using System.Windows.Forms;

namespace Fw
{
 public partial class frm1 : Form
    {
 private FileSystemWatcher watcher;
 private delegate void UpdateWatchTextDelegate(string newText);


 public frm1()
        {
            InitializeComponent();
 this.watcher = new FileSystemWatcher();
 this.watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
 this.watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
 this.watcher.Changed += new FileSystemEventHandler(watcher_Changed);
 this.watcher.Created += new FileSystemEventHandler(watcher_Created);

        }

 public void UpdateWatchText(string newText)
        {
            lblWatch.Text = newText;
        }


 public void WriteLog(string LogContent) 
        {
 using (StreamWriter sw = new StreamWriter("c:\\Log.txt", true))
            {
                sw.WriteLine(LogContent);
                sw.Close();
            } 
 
        }

 void watcher_Created(object sender, FileSystemEventArgs e)
        {           
 try
            {
                WriteLog(String.Format("File: {0} Created", e.FullPath));
 this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "文件" + e.FullPath + "被创建");
            }
 catch (IOException)
            {
 this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "创建日志写入失败!");
            }
        }


 void watcher_Changed(object sender, FileSystemEventArgs e)
        {            
 try
            {              
                WriteLog(String.Format("File: {0} {1}", e.FullPath, e.ChangeType.ToString()));
 this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "文件" + e.FullPath + "被修改");
            }
 catch (IOException)
            {
 this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "修改日志写入失败!");
            }
        }

 void watcher_Renamed(object sender, RenamedEventArgs e)
        {            
 try
            {               
                WriteLog(String.Format("File renamed from {0} to {1}", e.OldName, e.FullPath));
 this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "文件" + e.OldName + "被重命名为" + e.FullPath);
            }
 catch (IOException)
            {
 this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "重命名日志写入失败!");
            }
        }

 void watcher_Deleted(object sender, FileSystemEventArgs e)
        {            
 try
            {                
                WriteLog(String.Format("File: {0} Deleted", e.FullPath));
 this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "文件" + e.FullPath + "被删除");
            }
 catch (IOException)
            {
 this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "删除日志写入失败!");
            }
        }


 private void cmdBrowse_Click(object sender, EventArgs e)
        {
 if (this.folderBrowserDialog1.ShowDialog() != DialogResult.Cancel)
            {
                txtLocation.Text = this.folderBrowserDialog1.SelectedPath;
                cmdWatch.Enabled = true;
            }
        }

 private void cmdWatch_Click(object sender, EventArgs e)
        {
 if (txtLocation.Text.Length <= 0) 
            {
                MessageBox.Show("请先选择要监视的文件夹!");
                cmdBrowse.Focus();
 return;
            }
            watcher.Path = txtLocation.Text;//监控路径(文件夹)
            watcher.Filter = "*.*";//如果filter为文件名称则表示监控该文件,如果为*.txt则表示要监控指定目录当中的所有.txt文件
            watcher.NotifyFilter = NotifyFilters.LastWrite |
                NotifyFilters.FileName |
                NotifyFilters.Size;
            lblWatch.Text = watcher.Path + " 监视中";

 //begin watching.
            watcher.EnableRaisingEvents = true;
        }

 private void btnStop_Click(object sender, EventArgs e)
        {
            watcher.EnableRaisingEvents = false;
            lblWatch.Text = watcher.Path + " 监视已经停止!";
        }




    }
}

注:如果目录下还有子目录,FileWatcher默认情况下并不能监视到子目录下的文件,可以通过设置watcher.IncludeSubdirectories = true; 解决这个问题

源代码下载:http://files.cnblogs.com/yjmyzz/FileWatcher.rar

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2009-06-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档