zoukankan      html  css  js  c++  java
  • c#之监控文件结构

    如果需要知道修改文件或目录的时间,可以通过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;
            }
    
        }
    }
  • 相关阅读:
    小结Fragment与FragmentPagerAdapter的生命周期及其关系
    Android的Touch事件分发机制简单探析
    个人项目开源之c++基于epoll实现高并发游戏盒子(服务端+客户端)源代码
    个人项目开源之Django文件中转站源代码
    个人项目开源之Django图书借阅系统源代码
    个人项目开源之实现矩形树图代码统计工具源代码
    颜色渐变算法
    echarts玩转图表之矩形树图
    解决MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk.问题
    引号有几种
  • 原文地址:https://www.cnblogs.com/lfxiao/p/6766795.html
Copyright © 2011-2022 走看看