欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#利用FileSystemWatcher實(shí)時(shí)監(jiān)控文件的增加,修改,重命名和刪除

 更新時(shí)間:2022年08月07日 16:47:26   作者:xgh815  
好多時(shí)候,我們都需要知道某些目錄下的文件什么時(shí)候被修改、刪除過(guò)等。本文將利用FileSystemWatcher實(shí)現(xiàn)實(shí)時(shí)監(jiān)控文件的增加,修改,重命名和刪除,感興趣的可以了解一下

好多時(shí)候,我們都需要知道某些目錄下的文件什么時(shí)候被修改、刪除過(guò)等,如果能用miniFilter驅(qū)動(dòng)過(guò)濾來(lái)做的話當(dāng)然是最好不過(guò)了,這是內(nèi)核級(jí)別的,當(dāng)然也比較復(fù)雜。如果只是簡(jiǎn)單的記錄就沒(méi)必要用驅(qū)動(dòng)過(guò)濾級(jí)別的來(lái)做了,用FileSystemWatcher來(lái)做就要簡(jiǎn)單得多。

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)變更過(guò)程發(fā)生錯(cuò)誤

下面我們一起來(lái)完成一個(gè)文件監(jiān)控實(shí)例。

一、實(shí)例化FileSystemWatcher類,并注冊(cè)監(jiān)聽(tīng)事件

  //創(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)聽(tīng)事件,Created、Changed、Deleted三個(gè)事件傳遞的參數(shù)是一樣的,我們就用同一個(gè)方法來(lái)處理就可以了
            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等,看名就明白是什么了,這里不做過(guò)多解釋。

 //創(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è)問(wèn)題:因?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ù)庫(kù)中,這里就增加一個(gè)listView來(lái)展示就好了

代碼:

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)文章!

相關(guān)文章

最新評(píng)論