C#中FileSystemWatcher的使用教程

開局一張圖,在 System.IO 下的 FileSystemWatcher 常用于監(jiān)視文件系統(tǒng)的變更,當(dāng)文件系統(tǒng)中的文件或者文件夾被修改會自動觸發(fā)相應(yīng)的回調(diào)事件。
為了能夠了解 FileSystemWatcher 是怎么運(yùn)作的,你可以指定一個被監(jiān)視的文件夾,當(dāng)被監(jiān)視的文件夾修改后,大概會觸發(fā)如下的一些事件。
- Changed: 當(dāng)文件或者文件夾已經(jīng)完成修改時觸發(fā)此事件
- Created:當(dāng)文件或者文件夾已經(jīng)成功創(chuàng)建觸發(fā)此事件
- Deleted:當(dāng)文件或者文件夾已經(jīng)成功刪除觸發(fā)此事件
- Error:當(dāng)變更的過程中發(fā)生錯誤觸發(fā)此事件。
- Renamed:當(dāng)文件或者文件夾已經(jīng)成功被重命名時觸發(fā)此事件
創(chuàng)建一個簡單的 file 監(jiān)視
接下來我們在 Visual Studio 中創(chuàng)建一個 Console 程序,用來了解如何進(jìn)行文件監(jiān)視,不過建議把 Console 部署成 Windows Service,這樣方便在系統(tǒng)中常駐監(jiān)控,一旦被監(jiān)視的路徑發(fā)生變更就會自動發(fā)出通知事件。
該說的都說了,接下來一起研究代碼吧。
static void Main(string[] args)
{
string path = @"D:\IDG";
MonitorDirectory(path);
Console.ReadKey();
}
下面的代碼片段展示了 MonitorDirectory 方法的內(nèi)部邏輯,這個方法可用于監(jiān)視指定的文件夾并且當(dāng)文件夾變更時觸發(fā)事件,可以看到文件夾路徑是通過參數(shù)傳進(jìn)去的。
private static void MonitorDirectory(string path)
{
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Path = path;
fileSystemWatcher.Created += FileSystemWatcher_Created;
fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
fileSystemWatcher.EnableRaisingEvents = true;
}
可以著重了解一下上面的 event 是如何被灌入的,而且我還寫了一句 fileSystemWatcher.EnableRaisingEvents = true, 這是什么意思呢?表示當(dāng)前的路徑正式開始被監(jiān)控,一旦監(jiān)控的路徑出現(xiàn)變更,F(xiàn)ileSystemWatcher 中的指定事件將會被觸發(fā)。
掃一下上面定義的各個 event 事件,分別都定義了該事件的處理函數(shù),比如說:FileSystemWatcher_Created,F(xiàn)ileSystemWatcher_Renamed,FileSystemWatcher_Deleted ,顯而易見觸發(fā)某一個事件就會觸發(fā)它的事件處理函數(shù),對吧,具體代碼如下:
private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File created: {0}", e.Name);
}
private static void FileSystemWatcher_Renamed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File renamed: {0}", e.Name);
}
private static void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File deleted: {0}", e.Name);
}
下面是完整的可供參考的代碼清單。
using System;
using System.IO;
namespace IDGFileSystemWatcher
{
class Program
{
static void Main(string[] args)
{
string path = @"D:\IDG";
MonitorDirectory(path);
Console.ReadKey();
}
private static void MonitorDirectory(string path)
{
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Path = path;
fileSystemWatcher.Created += FileSystemWatcher_Created;
fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
fileSystemWatcher.EnableRaisingEvents = true;
}
private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File created: {0}", e.Name);
}
private static void FileSystemWatcher_Renamed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File renamed: {0}", e.Name);
}
private static void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File deleted: {0}", e.Name);
}
}
}
假設(shè) IDG 文件夾是在 E 盤內(nèi),接下來把 Console 運(yùn)行起來,然后在 IDG 文件夾內(nèi)創(chuàng)建一個新文件,不出意外的話,你會觀察到這個新建的文件名將會出現(xiàn)在 控制臺 上,說明 FileSystemWatcher_Created 被成功觸發(fā),參考下圖:

譯文鏈接:https://www.infoworld.com/article/3185447/how-to-work-with-filesystemwatcher-in-c.html
到此這篇關(guān)于C#中FileSystemWatcher使用教程的文章就介紹到這了,更多相關(guān)C# FileSystemWatcher使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# WinForm實現(xiàn)鼠標(biāo)穿透功能
在WinForm開發(fā)時,會用到這樣一個場景,給屏幕增加水印Logo,但不影響畫面的操作,這里就會用到鼠標(biāo)穿透功能,下面我們就來學(xué)習(xí)一下鼠標(biāo)穿透功能的具體實現(xiàn)吧2023-11-11
C#實現(xiàn)IDbConnection/IDbCommand等相關(guān)通用數(shù)據(jù)接口
ADO.NET?中的數(shù)據(jù)提供者對象提供了IDbConnection、IDbCommand、IDbDataParameter等通用數(shù)據(jù)接口,本文將利用這些對象實現(xiàn)一個通用方法以訪問和操作數(shù)據(jù)庫內(nèi)容,需要的朋友可以參考下2024-04-04
詳解C# Protobuf如何做到0分配內(nèi)存的序列化
這篇文章主要介紹了詳解C# Protobuf如何做到0分配內(nèi)存的序列化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
json格式數(shù)據(jù)分析工具PageElement類分享(仿Session寫法)
json格式數(shù)據(jù)分析工具PageElement類分享,可像Session一樣自由獲取Json元素的Key與Value。并可方便與ADO進(jìn)行交互2013-12-12

