公交车上荫蒂添的好舒服的电影-公用玩物(np双xing总受)-公用小荡货芊芊-公与妇仑乱hd-攻把受做哭边走边肉楼梯play-古装一级淫片a免费播放口

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

C#-使用FileSystemWatcher類觀察監(jiān)測系統(tǒng)文件和目錄的更改

admin
2023年12月26日 22:20 本文熱度 1166

FileSystemWatcher類

FileSystemWatcher類的主要功能:
監(jiān)控指定文件或目錄的文件的創(chuàng)建、刪除、改動、重命名等活動。可以動態(tài)地定義需要監(jiān)控的文件類型及文件屬性改動的類型。
具體可以參考微軟官方文檔FileSystemWatcher
Namespace:
System.IO
Assembly:
System.dll
Listens to the file system change notifications and raises events when a directory, or file in a directory, changes.

觀察文件的更改

使用FileSystemWatcher可以監(jiān)視文件的更改。事件在創(chuàng)建、重命名、刪除和更改文件時觸發(fā)。這可用于如下場景:需要對文件的變更作出反應(yīng)。例如,服務(wù)器上傳文件時,或文件緩存在內(nèi)存中,而緩存需要在文件更改時失效。
下面是一個FileMonitor的示例程序:

using System;

using System.IO;


namespace FileMonitor

{

    public class Program

    {

        private static FileSystemWatcher s_watcher;


        public static void Main(string[] args)

        {

        // 監(jiān)視當(dāng)前目錄下所有的txt文件

            WatchFiles(".", "*.txt");

            Console.ReadLine();

            UnWatchFiles();

        }


        public static void WatchFiles(string path, string filter)

        {

            s_watcher = new FileSystemWatcher(path, filter)

            {

                IncludeSubdirectories = true

            };

            s_watcher.Created += OnFileChanged;

            s_watcher.Changed += OnFileChanged;

            s_watcher.Deleted += OnFileChanged;

            s_watcher.Renamed += OnFileRenamed;

// 啟用事件的觸發(fā),這一點非常重要

            s_watcher.EnableRaisingEvents = true;

            Console.WriteLine("watching file changes...");

        }


        public static void UnWatchFiles()

        {

            s_watcher.Created -= OnFileChanged;

            s_watcher.Changed -= OnFileChanged;

            s_watcher.Deleted -= OnFileChanged;

            s_watcher.Renamed -= OnFileRenamed;

            s_watcher.Dispose();

            s_watcher = null;

        }


        private static void OnFileRenamed(object sender, RenamedEventArgs e) =>

            Console.WriteLine($"file {e.OldName} {e.ChangeType} to {e.Name}");


        private static void OnFileChanged(object sender, FileSystemEventArgs e) =>

            Console.WriteLine($"file {e.Name} {e.ChangeType}");

    }

}

微軟官方示例程序

The following example creates a FileSystemWatcher to watch the directory specified at run time. The component is set to watch for changes in LastWrite and LastAccess time, the creation, deletion, or renaming of text files in the directory. If a file is changed, created, or deleted, the path to the file prints to the console. When a file is renamed, the old and new paths print to the console.

using System;

using System.IO;

using System.Security.Permissions;


public class Watcher

{

    public static void Main()

    {

        Run();

    }


    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]

    private static void Run()

    {

        string[] args = Environment.GetCommandLineArgs();


        // If a directory is not specified, exit program.

        if (args.Length != 2)

        {

            // Display the proper way to call the program.

            Console.WriteLine("Usage: Watcher.exe (directory)");

            return;

        }


        // Create a new FileSystemWatcher and set its properties.

        using (FileSystemWatcher watcher = new FileSystemWatcher())

        {

            watcher.Path = args[1];


            // Watch for changes in LastAccess and LastWrite times, and

            // the renaming of files or directories.

            watcher.NotifyFilter = NotifyFilters.LastAccess

                                 | NotifyFilters.LastWrite

                                 | NotifyFilters.FileName

                                 | NotifyFilters.DirectoryName;


            // Only watch text files.

            watcher.Filter = "*.txt";


            // Add event handlers.

            watcher.Changed += OnChanged;

            watcher.Created += OnChanged;

            watcher.Deleted += OnChanged;

            watcher.Renamed += OnRenamed;


            // Begin watching.

            watcher.EnableRaisingEvents = true;


            // Wait for the user to quit the program.

            Console.WriteLine("Press 'q' to quit the sample.");

            while (Console.Read() != 'q') ;

        }

    }


    // Define the event handlers.

    private static void OnChanged(object source, FileSystemEventArgs e) =>

        // Specify what is done when a file is changed, created, or deleted.

        Console.WriteLine($"File: {e.FullPath} {e.ChangeType}");


    private static void OnRenamed(object source, RenamedEventArgs e) =>

        // Specify what is done when a file is renamed.

        Console.WriteLine($"File: {e.OldFullPath} renamed to {e.FullPath}");

}

參考資料


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

主站蜘蛛池模板: 国产精品美脚玉足脚交欧美 | 成人精品区天堂 | 精品久久久久久无码专区不久久 | 国产喷水大秀在线观看2025 | 国色天香成人一区二区 | 国产综合激情在线亚 | 国产在线精品二区 | 国产高清不卡一区二区三区 | 国产高清乱理伦片中文 | 韩国日韩欧美久久久 | 国产成a人片在线观看视频99 | 国产精品欧美亚洲韩国日本久久 | 国产欧美日韩制服 | 国产精品无码免费播放 | 国产激情偷乱视频一区二区 | a级毛片在线免费观看 | av一区二区三区电影在线播放 | 国产精品熟女四五十路一区二区 | 2025国精品产露脸偷拍视频 | 国产福利萌白酱精品tv一区 | 国产精品一区二区久久精品不卡 | 国产精品久在线观看观看视频 | 国产成人啪精品午夜在线观看 | 精品不卡免费一区二区三区 | 潮喷失禁大喷水av无码 | 国产亚洲欧美日韩亚洲中文色 | 国产真人无码作爱视频免费95 | 国产成人综合在线视 | 91在线| 爆乳高潮喷水无码正在播放 | 国产在线拍小情侣国产拍拍偷 | 国产精品自拍小视频 | 精品无码喷水 | 国产精品国三级国 | 丰满的继牳3中文字幕系列免费 | 91精品福利大全在线观看 | 国产精品国内免费一区二区三区 | 91精品云霸高清中文字幕 | 精品国免费一区二区三区 | 国产精品一级无码视频播放 | 国产无码午夜不卡 |