前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >FileSystemWatcher 监控文件变化

FileSystemWatcher 监控文件变化

作者头像
全栈程序员站长
发布2022-07-04 14:11:28
7880
发布2022-07-04 14:11:28
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

本文测试了FileSystemWatcher 类监控文件变化。

代码语言:javascript
复制
using System;
using System.Security.Permissions;
using System.IO;

namespace ConsoleApp1
{
    public class FileStateWatcher
    {    
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static int Run()
        {
            FileSystemWatcher fsWatcher = new FileSystemWatcher();
            fsWatcher.Path = "E:\\Test";

            fsWatcher.NotifyFilter = NotifyFilters.LastAccess |    //上一次打开的日期。 
                                     NotifyFilters.LastWrite |     //上一次写入内容的日期
                                     NotifyFilters.FileName |      //文件名
                                     NotifyFilters.DirectoryName | //目录名
                                     NotifyFilters.Size;           //大小

            //监听子目录
            fsWatcher.IncludeSubdirectories = true;
            //监听文件类型
            fsWatcher.Filter = "*.txt";

            //添加事件处理
            fsWatcher.Changed += new FileSystemEventHandler(OnChanged);
            fsWatcher.Created += new FileSystemEventHandler(OnCreated);
            fsWatcher.Deleted += new FileSystemEventHandler(OnDeleted);
            fsWatcher.Renamed += new RenamedEventHandler(OnRenamed);

            fsWatcher.EnableRaisingEvents = true;       
            return 0;
        }
        //修改时的处理
        private static void OnChanged(Object source, FileSystemEventArgs e)
        {
            Console.WriteLine("File: {0} {1}", e.FullPath, e.ChangeType);
        }
        //重命名时的处理
        private static void OnRenamed(Object source, FileSystemEventArgs e)
        {
            Console.WriteLine("File: {0} {1}", e.FullPath, e.ChangeType);
        }
        //删除时的处理
        private static void OnDeleted(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("File: {0} {1}", e.FullPath, e.ChangeType);
        }
        //创建时的处理
        private static void OnCreated(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("File: {0} {1}", e.FullPath, e.ChangeType);
        }
    };

    class Program
    {
        static void Main(string[] args)
        {
            FileStateWatcher.Run();
            // 输入q结束程序
            Console.WriteLine("Press q to quit the sample.");
            while (Console.Read() != 'q') ;
        }
    }
}

上例中监控的目录是“E:\\Test”,在此目录下创建txt文件,命名为“log.txt”

FileSystemWatcher 监控文件变化
FileSystemWatcher 监控文件变化

运行结果:

FileSystemWatcher 监控文件变化
FileSystemWatcher 监控文件变化

本例仅仅打印了发生变化的文件名及变化类型。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/149390.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档