首页 > 代码库 > 利用FileSystemWatcher实现磁盘文件监控

利用FileSystemWatcher实现磁盘文件监控

马上放假了,好开森啊O(∩_∩)O哈哈~

———————————————————————————————————————————————————————

昨天逛园子,发现了一个FileSystemWatcher的东西,大概看了一下,完全可以在后台自动监控文件的变化,深深后悔以前没有发现啊,简直为我省去了一大段代码啊……

节前工作不是很多,就尝试着用了一下,不过发现还有些问题,就是事件会重复触发,以后再解决吧。就不多罗嗦了……

贴代码才是王道啊

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace filewatcher{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        string pt;//file path        FileSystemWatcher watcher = new FileSystemWatcher();        private void Form1_Load(object sender, EventArgs e)        {            Control.CheckForIllegalCrossThreadCalls = false;            radioButton1.Checked = true;            ListView lst = new ListView();        }        private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)        {            FolderBrowserDialog path = new FolderBrowserDialog();            path.ShowDialog();            this.textBox1.Text = path.SelectedPath.ToString();            this.textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Regular);            this.textBox1.TextAlign =HorizontalAlignment.Left;            this.textBox1.ForeColor = Color.Black;            pt = textBox1.Text;                   }        private void toolStripButton1_Click(object sender, EventArgs e)        {                if (radioButton1.Checked)                    pt = "C:\\";                if (radioButton2.Checked)                    pt = "D:\\";                if (radioButton3.Checked)                    pt = "E:\\";                if (radioButton4.Checked)                    pt = "F:\\";                           filewatcher();               groupBox1.Enabled = false;          }        private void toolStripButton2_Click(object sender, EventArgs e)        {            watcher.EnableRaisingEvents = false;            groupBox1.Enabled = true;        }               private void filewatcher()        {            try            {                                if (flag == 1)                {                    watcher.Path = @pt;                    watcher.IncludeSubdirectories = true;//全局文件监控,包括子目录                    watcher.EnableRaisingEvents = true;//启用文件监控                    watcher.Created += new FileSystemEventHandler(OnCreat);                    watcher.Deleted += new FileSystemEventHandler(OnDelet);                    watcher.Changed += new FileSystemEventHandler(OnChange);                }                else                {                    watcher.EnableRaisingEvents = false;                    return;                }            }            catch(Exception ex)            {                return;            }                    }         private void OnCreat(object sender, FileSystemEventArgs e)        {            ListViewItem li = new ListViewItem(DateTime.Now.ToString("HH:mm:ss"));            li.SubItems.Add(getname(e.Name.ToString()));            li.SubItems.Add(e.ChangeType.ToString());            li.SubItems.Add(e.FullPath.ToString());            lst.Items.Add(li);        }        private void OnDelet(object sender, FileSystemEventArgs e)        {            ListViewItem li = new ListViewItem(DateTime.Now.ToString("HH:mm:ss"));            li.SubItems.Add(getname(e.Name.ToString()));            li.SubItems.Add(e.ChangeType.ToString());            li.SubItems.Add(e.FullPath.ToString());            lst.Items.Add(li);        }        private void OnChange(object sender, FileSystemEventArgs e)        {            ListViewItem li = new ListViewItem(DateTime.Now.ToString("HH:mm:ss"));            li.SubItems.Add(getname(e.Name.ToString()));            li.SubItems.Add(e.ChangeType.ToString());            li.SubItems.Add(e.FullPath.ToString());            lst.Items.Add(li);        }        //处理,获得文件名        private string getname(string str)        {            try            {                string[] sttarr = str.Split(new char[] { Convert.ToChar("\\") });                int length = sttarr.Length;                string filename = sttarr[length-1];                return filename;            }            catch (Exception cc)            {                MessageBox.Show("文件路径不合法","提示");                return str;            }        }    }}

 运行界面

:突然发现点小问题,就是事件会重复触发。。。顺道求解决方案啊

利用FileSystemWatcher实现磁盘文件监控