文件监控

利用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     }
原文地址:https://www.cnblogs.com/simen-tan/p/5384787.html