您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“C#如何利用FileSystemWatcher實(shí)時(shí)監(jiān)控文件的增加,修改,重命名和刪除”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“C#如何利用FileSystemWatcher實(shí)時(shí)監(jiān)控文件的增加,修改,重命名和刪除”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
FileSystemWatcher組件可以監(jiān)視文件系統(tǒng),并在文件系統(tǒng)發(fā)生改變時(shí)作出反應(yīng)。FileSystemWatcher 常用于文件系統(tǒng)變更的監(jiān)控,當(dāng)被監(jiān)視的文件夾目錄被創(chuàng)建、修改、重命名或刪除時(shí),會觸發(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í)例。
//創(chuàng)建一個(gè)FileSystemWatcher,并設(shè)置其屬性 FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(); //設(shè)置監(jiān)控的路徑 fileSystemWatcher.Path = “監(jiān)控路徑”; //是否監(jiān)控指定路徑中的子目錄 fileSystemWatcher.IncludeSubdirectories = true; //啟用 fileSystemWatcher.EnableRaisingEvents = true; //注冊監(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 對象成員有: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í)就自動創(chuàng)建了一個(gè)線程,在事件處理的方法中需要使用委托的方式封送到主線程中處理。
//聲明傳遞文件Created、Changed、Deleted對象和委托,用于文件增加、刪除、修改時(shí)更新UI界面 private delegate void setLogDelegate(FileSystemEventArgs e);
監(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對象和委托,用于文件Renamed時(shí)更新UI界面 private delegate void setLogDelegate(FileSystemEventArgs e); //聲明傳遞文件Created、Changed、Deleted對象和委托,用于文件增加、刪除、修改時(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)控文件的增加,修改,重命名和刪除”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。