• 利用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;
                }
            }
        }
    }
    

     运行界面

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

  • 相关阅读:
    图与链表的深拷贝
    Codeforces Round #686(Div.3) [C- Sequence
    前缀和
    递归改非递归
    STL源码剖析笔记
    第六章 进程
    C++ 设计模式--模板模式、策略模式、观察者模式
    宏定义方式 进行枚举类型和枚举类型的相互转换
    Linux常见信号介绍
    git rebase 操作
  • 原文地址:https://www.cnblogs.com/dranched/p/4001334.html
Copyright © 2020-2023  润新知