C#利用FileSystemWatcher實(shí)時(shí)監(jiān)控文件的增加,修改,重命名和刪除
好多時(shí)候,我們都需要知道某些目錄下的文件什么時(shí)候被修改、刪除過等,如果能用miniFilter驅(qū)動(dòng)過濾來做的話當(dāng)然是最好不過了,這是內(nèi)核級(jí)別的,當(dāng)然也比較復(fù)雜。如果只是簡單的記錄就沒必要用驅(qū)動(dòng)過濾級(jí)別的來做了,用FileSystemWatcher來做就要簡單得多。
FileSystemWatcher組件可以監(jiān)視文件系統(tǒng),并在文件系統(tǒng)發(fā)生改變時(shí)作出反應(yīng)。FileSystemWatcher 常用于文件系統(tǒng)變更的監(jiān)控,當(dāng)被監(jiān)視的文件夾目錄被創(chuàng)建、修改、重命名或刪除時(shí),會(huì)觸發(fā)以下事件:
1.Created: 當(dāng)新建文件或者文件夾
2.Changed:當(dāng)文件或者文件夾已經(jīng)完成修改
3.Renamed:當(dāng)文件或者文件夾完成重命名
4.Deleted:當(dāng)文件或者文件夾被刪除
5.Error:當(dāng)變更過程發(fā)生錯(cuò)誤
下面我們一起來完成一個(gè)文件監(jiān)控實(shí)例。
一、實(shí)例化FileSystemWatcher類,并注冊(cè)監(jiān)聽事件
//創(chuàng)建一個(gè)FileSystemWatcher,并設(shè)置其屬性
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
//設(shè)置監(jiān)控的路徑
fileSystemWatcher.Path = “監(jiān)控路徑”;
//是否監(jiān)控指定路徑中的子目錄
fileSystemWatcher.IncludeSubdirectories = true;
//啟用
fileSystemWatcher.EnableRaisingEvents = true;
//注冊(cè)監(jiān)聽事件,Created、Changed、Deleted三個(gè)事件傳遞的參數(shù)是一樣的,我們就用同一個(gè)方法來處理就可以了
fileSystemWatcher.Changed += new FileSystemEventHandler(FileSystemWatcher_EventHandler);
fileSystemWatcher.Created += new FileSystemEventHandler(FileSystemWatcher_EventHandler);
fileSystemWatcher.Deleted += new FileSystemEventHandler(FileSystemWatcher_EventHandler);
fileSystemWatcher.Renamed += new RenamedEventHandler(FileSystemWatcher_Renamed);
fileSystemWatcher.Error += new ErrorEventHandler(FileSystemWatcher_Error);
二、事件處理
FileSystemEventArgs 對(duì)象成員有:Name、OldName、ChangeType、FullPath、OldFullPath等,看名就明白是什么了,這里不做過多解釋。
//創(chuàng)建一個(gè)FileSystemWatcher,并設(shè)置其屬性
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
//設(shè)置監(jiān)控的路徑
fileSystemWatcher.Path = “監(jiān)控路徑”;
//是否監(jiān)控指定路徑中的子目錄
fileSystemWatcher.IncludeSubdirectories = true;
//啟用
fileSystemWatcher.EnableRaisingEvents = true;
//Created、Changed、Deleted三個(gè)事件的處理方法
private static void FileSystemWatcher_EventHandler(object sender, FileSystemEventArgs e)
{
Invoke(new Action(new Action(() =>
{
Console.WriteLine(e.Name+e.FullPath);
})));
}
//重命名事件的處理方法
private void FileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
Invoke(new Action(new Action(() =>
{
Console.WriteLine(e.OldName+e.Name+e.FullPath);
})));
}
//錯(cuò)誤事件的處理方法
private void FileSystemWatcher_Error(object sender, ErrorEventArgs e)
{
Invoke(new Action(new Action(() =>
{
Console.WriteLine(e.ToString()));
})));
}這里需要注意一個(gè)問題:因?yàn)镕ileSystemWatcher類本身就是一個(gè)多線程的控件,在實(shí)例化一個(gè)FileSystemWatcher時(shí)就自動(dòng)創(chuàng)建了一個(gè)線程,在事件處理的方法中需要使用委托的方式封送到主線程中處理。
//聲明傳遞文件Created、Changed、Deleted對(duì)象和委托,用于文件增加、刪除、修改時(shí)更新UI界面 private delegate void setLogDelegate(FileSystemEventArgs e);
三、展示監(jiān)控記錄
監(jiān)控的記錄可以保存到文件和數(shù)據(jù)庫中,這里就增加一個(gè)listView來展示就好了

代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FileWatcher
{
public partial class Form1 : Form
{
private delegate void renameDelegate(RenamedEventArgs e); //聲明傳遞RenamedEventArgs對(duì)象和委托,用于文件Renamed時(shí)更新UI界面
private delegate void setLogDelegate(FileSystemEventArgs e); //聲明傳遞文件Created、Changed、Deleted對(duì)象和委托,用于文件增加、刪除、修改時(shí)更新UI界面
private FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
private ColumnHeader chTime = new ColumnHeader();
private ColumnHeader chEvent = new ColumnHeader();
private ColumnHeader chFile = new ColumnHeader();
private ColumnHeader chPath = new ColumnHeader();
public Form1()
{
InitializeComponent();
chTime.TextAlign = HorizontalAlignment.Center;
chTime.Width = 130;
chTime.Text = "時(shí)間";
this.listViewInfo.Columns.Add(chTime);
chEvent.TextAlign = HorizontalAlignment.Center;
chEvent.Width = 80;
chEvent.Text = "事件";
this.listViewInfo.Columns.Add(chEvent);
chFile.Width = 270;
chFile.Text = "文件";
this.listViewInfo.Columns.Add(chFile);
chPath.Width = this.listViewInfo.Width - chTime.Width - chEvent.Width - chFile.Width - 21;
chPath.Text = "位置";
this.listViewInfo.Columns.Add(chPath);
ColumnHeader chEnd = new ColumnHeader();
chEnd.Width = 17;
chEnd.Text = "";
this.listViewInfo.Columns.Add(chEnd);
this.listViewInfo.View = View.Details;
this.listViewInfo.GridLines = true;
fileSystemWatcher.Changed += new FileSystemEventHandler(FileSystemWatcher_EventHandler);
fileSystemWatcher.Created += new FileSystemEventHandler(FileSystemWatcher_EventHandler);
fileSystemWatcher.Deleted += new FileSystemEventHandler(FileSystemWatcher_EventHandler);
fileSystemWatcher.Renamed += new RenamedEventHandler(FileSystemWatcher_Renamed);
fileSystemWatcher.Error += new ErrorEventHandler(FileSystemWatcher_Error);
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.EnableRaisingEvents = true;
}
#region 文件增加、刪除、修改時(shí)被調(diào)用的處理方法
private void FileSystemWatcher_EventHandler(object sender, FileSystemEventArgs e)
{
if (listViewInfo.InvokeRequired) //判斷是否跨線程
{
listViewInfo.Invoke(new setLogDelegate(SetLog), new object[] { e });//使用委托將方法封送到UI主線程處理
}
}
private void SetLog(FileSystemEventArgs e)
{
string strLog = "";
switch (e.ChangeType.ToString())
{
case "Created":
strLog = "文件創(chuàng)建";
break;
case "Changed":
strLog = "文件修改";
break;
case "Deleted":
strLog = "文件刪除";
break;
default:
strLog = e.ChangeType.ToString();
break;
}
ListViewItem lvi = new ListViewItem();
lvi.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
lvi.SubItems.Add(strLog);
lvi.SubItems.Add(e.Name);
lvi.SubItems.Add(e.FullPath.Replace(e.Name,""));
listViewInfo.Items.Add(lvi);
}
#endregion
#region 重命名方法
private void FileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
if (listViewInfo.InvokeRequired)
{
listViewInfo.Invoke(new renameDelegate(SetRenamedLog), new object[]{e});
}
}
private void SetRenamedLog(RenamedEventArgs e)
{
//listViewInfo.Items.Add(string.Format("重命名:{0} 被換名為:{1}, {2}", e.OldName, e.Name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
ListViewItem lvi = new ListViewItem();
lvi.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
lvi.SubItems.Add("文件重命名");
lvi.SubItems.Add(e.OldName + "被換名為:" + e.Name);
lvi.SubItems.Add(e.FullPath.Replace(e.Name, ""));
listViewInfo.Items.Add(lvi);
}
#endregion
#region 錯(cuò)誤事件的方法
private void FileSystemWatcher_Error(object sender, ErrorEventArgs e)
{
if (listViewInfo.InvokeRequired) //判斷是否跨線程
{
//使用委托處理
Invoke(new Action(new Action(() =>
{
listViewInfo.Items.Add(string.Format("文件出錯(cuò):{0}, {1}", e.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
})));
}
}
#endregion
}
}以上就是C#利用FileSystemWatcher實(shí)時(shí)監(jiān)控文件的增加,修改,重命名和刪除的詳細(xì)內(nèi)容,更多關(guān)于C# FileSystemWatcher監(jiān)控文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- C#中FileSystemWatcher類實(shí)現(xiàn)監(jiān)控文件夾
- C# FileSystemWatcher 在監(jiān)控文件夾和文件時(shí)的使用方法
- C#使用FileSystemWatcher控件實(shí)現(xiàn)的文件監(jiān)控功能示例
- C#采用FileSystemWatcher實(shí)現(xiàn)監(jiān)視磁盤文件變更的方法
- c#使用filesystemwatcher實(shí)時(shí)監(jiān)控文件目錄的添加和刪除
- c#使用filesystemwatcher監(jiān)視文件系統(tǒng)的變化
- C#通過FileSystemWatcher監(jiān)聽文件的實(shí)戰(zhàn)技巧
相關(guān)文章
C# OpenCvSharp實(shí)現(xiàn)圖片批量改名
這篇文章主要為大家詳細(xì)介紹了C#如何結(jié)合OpenCvSharp實(shí)現(xiàn)圖片批量改名功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
C#實(shí)現(xiàn)合并多張圖片為GIF動(dòng)態(tài)圖
這篇文章主要為大家詳細(xì)介紹了C#如何將把一張又一張的圖片去拼合成一張GIF動(dòng)態(tài)圖片,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12
C#實(shí)現(xiàn)根據(jù)圖片的鏈接地址獲取圖片的后綴名
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)根據(jù)圖片的鏈接地址獲取圖片的后綴名,文中的實(shí)現(xiàn)方法講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02

