zhao123

有些时候,文件修改需要及时的响应,这个时候就需要实时读取文件,预先想的是写一个计时器,每隔多久运行一次,但是不能实时响应,所以采用监听文件的方式实现读取数据

C#监听文件变化

/// <summary>
/// 监听文件变化
/// </summary>
private static void WatcherFile()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = Environment.CurrentDirectory;
    watcher.Filter = "*.config";

    watcher.Changed += (object sender, FileSystemEventArgs e) => {
        Thread.Sleep(100);
        FetchData();
    };
    watcher.EnableRaisingEvents = true;
}

C#读取配置文件

读取配置文件中的数据一般用ConfigurationManager.AppSettings["key"],web的配置文件更新之后会实时更新, 应用程序的配置文件不会实时更新,更新应用程序的配置文件之后需刷新否则读取的还是原来的旧数据。

ConfigurationManager.RefreshSection("key");
ConfigurationManager.AppSettings["key"];

 

分类:

技术点:

相关文章:

  • 2018-09-07
  • 2021-11-04
  • 2021-09-19
  • 2021-04-14
  • 2021-11-15
  • 2021-09-17
  • 2019-11-26
  • 2021-08-27
猜你喜欢
  • 2017-11-23
  • 2021-09-17
  • 2021-09-08
  • 2021-09-17
  • 2019-03-06
  • 2021-09-02
  • 2018-02-05
相关资源
相似解决方案