欧美成人精品手机在线观看_69视频国产_动漫精品第一页_日韩中文字幕网 - 日本欧美一区二区

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

使用C#如何監(jiān)控選定文件夾中文件的變動(dòng)情況?

freeflydom
2025年3月5日 8:32 本文熱度 602

前言?

有時(shí)候我們會(huì)有監(jiān)控電腦上某一個(gè)文件夾中文件變動(dòng)情況的需求,在本文中,我也會(huì)以一個(gè)具體的例子,說(shuō)明在C#中如何使用FileSystemWatcher類來(lái)實(shí)現(xiàn)上述需求。

效果?

具體實(shí)現(xiàn)?

如果你對(duì)C#如何監(jiān)控選定文件夾中文件的變動(dòng)情況感興趣,可以繼續(xù)往下閱讀。

界面設(shè)計(jì)

為了更好的演示效果,我這里winform的界面設(shè)計(jì)如下:

很簡(jiǎn)單,只有一個(gè)button與一個(gè)richtextbox,button用來(lái)指定被監(jiān)控的文件,richtextbox用來(lái)輸出一些信息。

全部代碼

namespace FileSystemWatcherDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // 創(chuàng)建一個(gè) FolderBrowserDialog 實(shí)例
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            // 設(shè)置對(duì)話框的標(biāo)題
            folderBrowserDialog.Description = "選擇文件夾";
            // 如果用戶點(diǎn)擊了“確定”按鈕
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.Text = "";
                // 獲取用戶選擇的文件夾路徑
                string selectedFolder = folderBrowserDialog.SelectedPath;
                // 提示被監(jiān)控文件夾路徑
                richTextBox1.Text += $"被監(jiān)控的文件夾為:{selectedFolder}\r\n";
                var watcher = new FileSystemWatcher($"{selectedFolder}");
               
                watcher.NotifyFilter = NotifyFilters.Attributes
                                | NotifyFilters.CreationTime
                                | NotifyFilters.DirectoryName
                                | NotifyFilters.FileName
                                | NotifyFilters.LastAccess
                                | NotifyFilters.LastWrite
                                | NotifyFilters.Security
                                | NotifyFilters.Size;
                watcher.Changed += OnChanged;
                watcher.Created += OnCreated;
                watcher.Deleted += OnDeleted;
                watcher.Renamed += OnRenamed;
                
                watcher.Filter = "*.txt";
                watcher.IncludeSubdirectories = true;
                watcher.EnableRaisingEvents = true;
            }
            else
            {
                MessageBox.Show("您本次沒(méi)有選擇文件夾!!!");
            }
          
           
   
        }
        private void AppendMessageToRichTextBox(string message)
        {
            // 在RichTextBox中添加提示信息        
            richTextBox1.Invoke(new Action(() =>
            {
                richTextBox1.AppendText(message + Environment.NewLine);
            }));
        }
        private void OnChanged(object sender, FileSystemEventArgs e)
        {
            if (e.ChangeType != WatcherChangeTypes.Changed)
            {
                return;
            }
            AppendMessageToRichTextBox($"Changed: {e.FullPath}");
        }
        private void OnCreated(object sender, FileSystemEventArgs e)
        {
            string value = $"Created: {e.FullPath}";
            AppendMessageToRichTextBox($"Created: {e.FullPath}");
        }
        private  void OnDeleted(object sender, FileSystemEventArgs e)
        {
            AppendMessageToRichTextBox($"Deleted: {e.FullPath}");
        }
        private  void OnRenamed(object sender, RenamedEventArgs e)
        {       
            AppendMessageToRichTextBox($"Renamed:");
            AppendMessageToRichTextBox($"    Old: {e.OldFullPath}");
            AppendMessageToRichTextBox($"    New: {e.FullPath} ");           
        }
     
    }
}

FileSystemWatcher的介紹

看過(guò)以上代碼,會(huì)發(fā)現(xiàn)核心就是FileSystemWatcher的使用。接下來(lái)我將介紹一下C#中的FileSystemWatcher類。

FileSystemWatcher是C#中的一個(gè)類,該類可以偵聽(tīng)文件系統(tǒng)更改通知,并在目錄或目錄中的文件發(fā)生更改時(shí)引發(fā)事件。

FileSystemWatcher的構(gòu)造函數(shù)

該類有三種構(gòu)造函數(shù),如下所示:

形式含義
FileSystemWatcher()初始化 FileSystemWatcher 類的新實(shí)例。
FileSystemWatcher(String)初始化 FileSystemWatcher 類的新實(shí)例,給定要監(jiān)視的目錄。
FileSystemWatcher(String, String)初始化 FileSystemWatcher類的新實(shí)例,給定要監(jiān)視的目錄和文件類型。
 var watcher = new FileSystemWatcher($"{selectedFolder}");

本文中我選擇的就是第二種構(gòu)造函數(shù),指定要監(jiān)視的目錄。

FileSystemWatcher的屬性

現(xiàn)在介紹一下在本示例中用到的FileSystemWatcher的屬性,如下所示:

名稱類型含義
EnableRaisingEventsbool設(shè)置FileSystemWatcher是否有效
Filterstring設(shè)置一個(gè)要監(jiān)控的文件的格式
FiltersCollection設(shè)置多個(gè)要監(jiān)控的文件的格式
IncludeSubdirectoriesbool獲取或設(shè)置一個(gè)值,該值指示是否應(yīng)監(jiān)視指定路徑中的子目錄
NotifyFilterNotifyFilters獲取或設(shè)置要監(jiān)視的更改的類型
Pathstring獲取或設(shè)置要監(jiān)視的目錄的路徑

現(xiàn)在來(lái)解釋下所用到的代碼的含義:

watcher.Filter = "*.txt";

表示要監(jiān)控的文件為.txt格式。

 watcher.IncludeSubdirectories = true;

表示指定路徑中的子目錄也要監(jiān)視。

 watcher.EnableRaisingEvents = true;

表示該對(duì)象可以觸發(fā)事件,也就是還有效。

 watcher.NotifyFilter = NotifyFilters.Attributes
                                | NotifyFilters.CreationTime
                                | NotifyFilters.DirectoryName
                                | NotifyFilters.FileName
                                | NotifyFilters.LastAccess
                                | NotifyFilters.LastWrite
                                | NotifyFilters.Security
                                | NotifyFilters.Size;

設(shè)置要監(jiān)視的更改的類型。NotifyFilter屬性的類型為NotifyFilters枚舉類型。

NotifyFilters枚舉類型:

[System.Flags]
public enum NotifyFilters

指定要在文件或文件夾中監(jiān)視的更改。

此枚舉支持其成員值的按位組合。

該枚舉類型包含的值與含義如下所示:

名稱含義
Attributes文件或文件夾的屬性
CreationTime文件或文件夾的創(chuàng)建時(shí)間
DirectoryName目錄名
FileName文件的名稱
LastAccess文件或文件夾上一次打開(kāi)的日期
LastWrite上一次向文件或文件夾寫(xiě)入內(nèi)容的日期
Security文件或文件夾的安全設(shè)置
Size文件或文件夾的大小

在這里使用了該枚舉類型的按位組合表示這幾種更改的類型要受到監(jiān)視。

FileSystemWatcher的事件

FileSystemWatcher中的事件如下:

名稱含義
Changed當(dāng)更改指定 Path 中的文件和目錄時(shí)發(fā)生
Created當(dāng)在指定Path 中創(chuàng)建文件和目錄時(shí)發(fā)生
Deleted刪除指定Path中的文件或目錄時(shí)發(fā)生
Renamed重命名指定 Path中的文件或目錄時(shí)發(fā)生
Error當(dāng) FileSystemWatcher 的實(shí)例無(wú)法繼續(xù)監(jiān)視更改或內(nèi)部緩沖區(qū)溢出時(shí)發(fā)生
                watcher.Changed += OnChanged;
                watcher.Created += OnCreated;
                watcher.Deleted += OnDeleted;
                watcher.Renamed += OnRenamed;

在這里我使用到了Changed、Created、Deleted和Renamed事件。

我將以Changed 事件為例,詳細(xì)解釋一下:

 watcher.Changed += OnChanged;

這行代碼的含義。

我們查看FileSystemWatcher的源代碼,Changed事件的代碼如下所示:

/// <devdoc>
///    Occurs when a file or directory in the specified <see cref='System.IO.FileSystemWatcher.Path'/> is changed.
/// </devdoc>
public event FileSystemEventHandler? Changed
{
    add
    {
        _onChangedHandler += value;
    }
    remove
    {
        _onChangedHandler -= value;
    }
}

可知將值賦給了_onChangedHandler,我們?cè)賮?lái)查看_onChangedHandler的定義:

 // Event handlers
 private FileSystemEventHandler? _onChangedHandler;

類型為FileSystemEventHandler?與Changed事件一致,再來(lái)看看FileSystemEventHandler?的定義:

 public delegate void FileSystemEventHandler(object sender, FileSystemEventArgs e);

發(fā)現(xiàn)是一個(gè)參數(shù)類型分別為object、FileSystemEventArgs返回值類型為空的委托類型。

object我們知道,那么FileSystemEventArgs又是什么呢?

查看它的源碼,截取一部分,如下所示:

public class FileSystemEventArgs : EventArgs
{
     private readonly WatcherChangeTypes _changeType;
     private readonly string? _name;
     private readonly string _fullPath;
     /// <devdoc>
///    Gets one of the <see cref='System.IO.WatcherChangeTypes'/> values.
/// </devdoc>
public WatcherChangeTypes ChangeType
{
    get
    {
        return _changeType;
    }
}
/// <devdoc>
///    Gets the fully qualified path of the affected file or directory.
/// </devdoc>
public string FullPath
{
    get
    {
        return _fullPath;
    }
}
/// <devdoc>
///       Gets the name of the affected file or directory.
/// </devdoc>
public string? Name
{
    get
    {
        return _name;
    }
}
 }

發(fā)現(xiàn)FileSystemEventArgs繼承自EventArgs,而EventArgs表示包含事件數(shù)據(jù)的類的基類,因此可以明白FileSystemEventArgs表示為目錄事件:Changed, Created, Deleted提供數(shù)據(jù)的類。

FileSystemEventArgs提供三個(gè)數(shù)據(jù)分別為ChangeType、FullPath、Name。

那ChangeType是什么呢?

查看ChangeType的定義:

 //
 // 摘要:
 //     Changes that might occur to a file or directory.
 [Flags]
 public enum WatcherChangeTypes
 {
     //
     // 摘要:
     //     The creation of a file or folder.
     Created = 1,
     //
     // 摘要:
     //     The deletion of a file or folder.
     Deleted = 2,
     //
     // 摘要:
     //     The change of a file or folder. The types of changes include: changes to size,
     //     attributes, security settings, last write, and last access time.
     Changed = 4,
     //
     // 摘要:
     //     The renaming of a file or folder.
     Renamed = 8,
     //
     // 摘要:
     //     The creation, deletion, change, or renaming of a file or folder.
     All = 15
 }

是一個(gè)枚舉類型,表示更改的類型。

現(xiàn)在回過(guò)頭來(lái)看:

watcher.Changed += OnChanged;

OnChanged方法如下:

  private void OnChanged(object sender, FileSystemEventArgs e)
  {
      if (e.ChangeType != WatcherChangeTypes.Changed)
      {
          return;
      }
      AppendMessageToRichTextBox($"Changed: {e.FullPath}");
  }

為什么可以將OnChanged方法訂閱到watcher.Changed事件上呢?

因?yàn)镺nChanged方法與watcher.Changed事件中的委托類型FileSystemEventHandler的返回類型和簽名是相同的。

OnChanged方法的返回類型與簽名如下:

 private void OnChanged(object sender, FileSystemEventArgs e)

FileSystemEventHandler委托類型的定義如下:

 public delegate void FileSystemEventHandler(object sender, FileSystemEventArgs e);

現(xiàn)在已經(jīng)理解了訂閱事件,那么什么時(shí)候觸發(fā)事件呢?

查看FileSystemWatcher的部分源碼:

 /// <devdoc>
 ///    Raises the <see cref='System.IO.FileSystemWatcher.Changed'/> event.
 /// </devdoc>
 protected void OnChanged(FileSystemEventArgs e)
 {
     InvokeOn(e, _onChangedHandler);
 }
 private void InvokeOn(FileSystemEventArgs e, FileSystemEventHandler? handler)
 {
     if (handler != null)
     {
         ISynchronizeInvoke? syncObj = SynchronizingObject;
         if (syncObj != null && syncObj.InvokeRequired)
             syncObj.BeginInvoke(handler, new object[] { this, e });
         else
             handler(this, e);
     }
 }

當(dāng)發(fā)生相應(yīng)的改變時(shí),就會(huì)調(diào)用FileSystemWatcher類的OnChanged方法,從而觸發(fā)事件。

總結(jié)?

本文通過(guò)一個(gè)實(shí)例,介紹了如何通過(guò)C#中的FileSystemWatcher類實(shí)現(xiàn)監(jiān)控選定的文件夾,希望對(duì)你有所幫助。

?轉(zhuǎn)自https://www.cnblogs.com/mingupupu/p/17933098.html


該文章在 2025/3/5 8:32:12 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved