zoukankan      html  css  js  c++  java
  • 文件监控

    利用FileSystemWatcher类对文件的写、重命名、新建进行监控以及执行触发的事件,并记录对文件进行了什么操作。

    代码如下:

     1 public partial class Form1 : Form
     2     {
     3         private FileSystemWatcher watcher;
     4         private delegate void UpdateWatcherTextDelegate(string operationLog);//为异步调用执行委托里面的方法
     5         public Form1()
     6         {
     7             InitializeComponent();
     8             this.watcher = new FileSystemWatcher();
     9             this.watcher.Deleted += new FileSystemEventHandler(this.OnDelete);
    10             this.watcher.Changed += new FileSystemEventHandler(this.OnChange);
    11             this.watcher.Created += new FileSystemEventHandler(this.OnCreate);
    12             this.watcher.Renamed += new RenamedEventHandler(this.OnRename);
    13         }
    14 
    15         public void UpdateWatchText(string text) 
    16         {
    17             label1.Text = text;
    18         }
    19 
    20         public void OnDelete(object source, FileSystemEventArgs e) 
    21         {
    22             EventData("Write a delete log to text", e);
    23         }
    24      
    25         public void OnChange(object source, FileSystemEventArgs e)
    26         {
    27             EventData("Write a change log to text", e);
    28         }
    29 
    30         public void OnRename(object source, RenamedEventArgs e)
    31         {
    32             EventData("Write a rename log to text",e);
    33         }
    34 
    35         public void OnCreate(object source, FileSystemEventArgs e)
    36         {
    37             EventData("Write a create log to text", e);
    38         }
    39 
    40         private void EventData(string strWrite, FileSystemEventArgs e)
    41         {
    42             FileStream fs = new FileStream("Log.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
    43             StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
    44             try
    45             {
    46                 sw.WriteLine(string.Format("File:{0},{1}", e.FullPath, e.ChangeType.ToString()));
    47                 sw.Close();
    48                 this.BeginInvoke(new UpdateWatcherTextDelegate(UpdateWatchText), strWrite);
    49             }
    50             catch (IOException io)
    51             {
    52                 this.BeginInvoke(new UpdateWatcherTextDelegate(UpdateWatchText), "Error have happened:" + io.ToString());
    53             }
    54         }
    55         private void button1_Click(object sender, EventArgs e)
    56         {
    57             if (openFileDialog1.ShowDialog() == DialogResult.OK) 
    58             {
    59                 textBox1.Text = openFileDialog1.FileName;
    60             }
    61         }
    62 
    63         private void button2_Click(object sender, EventArgs e)
    64         {
    65             this.watcher.Path = Path.GetDirectoryName(textBox1.Text);
    66             this.watcher.Filter = Path.GetFileName(textBox1.Text);
    67             watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName | NotifyFilters.Size;
    68             this.watcher.EnableRaisingEvents = true;
    69         }
    70 
    71         
    72     }
  • 相关阅读:
    【JavaScript】WebBrowser控件下IE版本的检测
    【ArcGIS】Oracle RAC下创建地理数据库(Create Enterprise Geodatabase)失败的解决方法
    【IOS】从零开始搭建基于Xcode7的IOS开发环境和免开发者帐号真机调试运行第一个IOS程序HelloWorld
    【Delphi】解决Delphi Distiller运行报错"HKEY_CURRENT_USER\" is of wrong kind or size
    【Oracle】根据字段值全库搜索相关数据表和字段
    【Android】彻底去除Google AdMob广告
    【Android】豆瓣FM离线数据
    【Android】反编译apk + eclipse中调试smali
    关于React前端构建的一般过程
    Consul入门
  • 原文地址:https://www.cnblogs.com/simen-tan/p/5384787.html
Copyright © 2011-2022 走看看