文件监控

本文介绍如何利用C#中的FileSystemWatcher类来监控文件的变化,包括文件被删除、修改、创建及重命名等操作,并记录这些操作的日志。通过设置事件处理函数,可以对文件系统中的特定事件做出响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值