监控文件夹的变化

阅读更多
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace FileWatch
{
    public partial class FrmFileWatch : Form
    {
        /// 
        /// 监控文件夹的对象
        /// 
        private readonly FileSystemWatcher fileWatcher;

        public FrmFileWatch()
        {
            InitializeComponent();

            fileWatcher = new FileSystemWatcher();
            //当 Changed、Created、Deleted 和 Renamed 事件由可见 Windows 窗体组件(如 Button)处理时,通过系统线程池访问组件可能不起作用或者导致异常。若要避免这种情况,请将 SynchronizingObject 设置为 Windows 窗体组件,这样将引起在创建组件的同一线程上调用处理 Changed、Created、Deleted 和 Renamed 事件的方法。
            fileWatcher.SynchronizingObject = this;

            //注册事件
            fileWatcher.Created += fileWatcher_Created;
            fileWatcher.Deleted += fileWatcher_Deleted;
            fileWatcher.Changed += fileWatcher_Changed;
            fileWatcher.Renamed += fileWatcher_Renamed;
        }

        #region 监控文件夹回调函数
        /// 
        /// 创建回调函数
        /// 
        /// 
        /// 
        private void fileWatcher_Created(object sender, FileSystemEventArgs e)
        {
            lbChangedFile.Items.Add("创建  " + e.FullPath);
            lbChangedFile.SelectedIndex = lbChangedFile.Items.Count -1;
        }
        /// 
        /// 删除回调函数
        /// 
        /// 
        /// 
        private void fileWatcher_Deleted(object sender, FileSystemEventArgs e)
        {
            lbChangedFile.Items.Add("删除  "+ e.FullPath);
            lbChangedFile.SelectedIndex = lbChangedFile.Items.Count - 1;
        }
        /// 
        /// 修改回调函数
        /// 
        /// 
        /// 
        private void fileWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            lbChangedFile.Items.Add("修改  "+ e.FullPath);
            lbChangedFile.SelectedIndex = lbChangedFile.Items.Count - 1;
        }
        /// 
        /// 重命名回调函数
        /// 
        /// 
        /// 
        private void fileWatcher_Renamed(object sender, RenamedEventArgs e)
        {
            lbChangedFile.Items.Add(e.OldFullPath+"  重命名为  "+ e.FullPath);
            lbChangedFile.SelectedIndex = lbChangedFile.Items.Count - 1;
        }
        #endregion

        #region 浏览
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            if (DialogResult.OK != folderBrowserDialog.ShowDialog()) return;

            string path = folderBrowserDialog.SelectedPath;
            if (!string.IsNullOrEmpty(path.Trim()))
            {
                txtPath.Text = path;
            }
        }
        #endregion

        #region 监控
        private void btnFileWatch_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(txtPath.Text.Trim()))
            {
                MessageBox.Show(@"该文件夹不存在!", @"信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //开始监控指定的文件夹
            fileWatcher.Path = txtPath.Text.Trim();
            fileWatcher.EnableRaisingEvents = true;

            MessageBox.Show(string.Format(@"文件夹【{0}】已处于监控状态!", txtPath.Text.Trim()), @"信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        #endregion

        #region 窗体关闭时释放资源
        private void FrmFileWatch_FormClosed(object sender, FormClosedEventArgs e)
        {
            fileWatcher.EnableRaisingEvents = false;
            fileWatcher.Dispose();
        }
        #endregion
    }
}

 

  • FileWatch.rar (41.1 KB)
  • 下载次数: 3

你可能感兴趣的:(Windows)