.net c# 监听文件事件

12 篇文章 0 订阅
10 篇文章 0 订阅

今天在学习asp.net core 的配置文件相关知识时,了解到AddJsonFile的reloadOnChange参数设置为true则配置文件发生变化后,可以重新加载配置。因为没有这知识,之前做winform端自定义配置时,都是文件启动时加在一次配置,没法实时修改配置后生效。现在记录下来以便以后可以使用。
参考 码农阿宇-浅析 .Net Core中Json配置的自动更新

asp.net core 配置文件设置

   #region Configure 用于定义请求管道中的中间件
        /// <summary>
        /// 用于定义请求管道中的中间件
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        public void Configure(IApplicationBuilder app,IHostingEnvironment env)
        {
            ConfigurationSet(env);
            //...隐藏其他中间件
        }

      #region ConfigurationSet 设置配置文件
        /// <summary>
        /// 设置配置文件
        /// </summary>
        /// <param name="env"></param>
        private static void ConfigurationSet(IHostingEnvironment env)
        {
            //配置文件:如果同个配置在多个配置文件中同时存在,则最后的配置文件里的配置生效
            var _builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json",optional: true,reloadOnChange: true)//从json文件中取值,reloadOnChange:当配置文件发生变化后,是否重新加载配置
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json",optional: true);//在开发环境中会使用“appsettings.Development.json”的配置文件,并用其中的值覆盖当前存在的其他值。
           
            _builder.AddEnvironmentVariables();//从环境变量中取值
            SysConfig.WebCongfig = _builder.Build();
        }
        #endregion

文件监听

using System.Diagnostics;
using System.IO;
using Newtonsoft.Json;

namespace LchCommon
{
    public class PhysicalFilesWatcher
    {
        //=================== construct function ==================	
        #region construct construct
        /// <summary>
        /// construct
        /// </summary>
        public PhysicalFilesWatcher()
        {
            //FileSystemWatcher(string path, string filter):path:目录,filter:默认 *.*,
            this.m_fileWatcher = new FileSystemWatcher("f:\\lch","lch.txt")
            {
                IncludeSubdirectories = true//是否监视子目录
            };
            this.m_fileWatcher.Created += new FileSystemEventHandler(this.OnChanged);
            this.m_fileWatcher.Changed += new FileSystemEventHandler(this.OnChanged);
            this.m_fileWatcher.Renamed += new RenamedEventHandler(this.OnRenamed);
            this.m_fileWatcher.Deleted += new FileSystemEventHandler(this.OnChanged);
            this.m_fileWatcher.Error += new ErrorEventHandler(this.OnError);
            this.m_fileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.Size;//监听类型
            this.m_fileWatcher.EnableRaisingEvents = true;//开启监听
        }
        #endregion



        //=================== private fields ======================
        #region fields 
        private FileSystemWatcher m_fileWatcher;
        #endregion


        //=================== event metod =========================
        #region OnError 出错
        /// <summary>
        /// 出错
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnError(object sender,ErrorEventArgs e)
        {
            Debug.WriteLine("OnError");
        }
        #endregion

        #region OnRenamed 重命名
        /// <summary>
        /// 重命名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnRenamed(object sender,RenamedEventArgs e)
        {
            Debug.WriteLine("OnRenamed");
        }
        #endregion

        #region OnChanged 改变事件
        /// <summary>
        /// 改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnChanged(object sender,FileSystemEventArgs e)
        {
            Debug.WriteLine("OnChanged");
            Debug.WriteLine(JsonConvert.SerializeObject(e));
        }
        #endregion
    }
}

触发修改事件的参数
在这里插入图片描述

参考 [码农阿宇-浅析 .Net Core中Json配置的自动更新]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值