Librarie读取检测文件更改的配置文件(xml或json)

问题描述 投票:0回答:1

我有这样的XML配置文件:

<configuration>
<items>
    <item key="KEY_NAME">
        3
    </item>
</items>
</configuration>

我需要一些库来读取(仅读取)文件的值,但无需重新启动应用程序就可以检测到更改(例如,将值3更改为5)。我当时使用njupiter,但该项目已弃用。我找到了MiniFSWatcher,您还认识其他图书馆吗?

谢谢,

c# asp.net config readfile xml-configuration
1个回答
1
投票
为什么不使用官方的https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=netframework-4.8类来监视文件系统?

以下示例:

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 = "*.xml"; // Add event handlers. watcher.Changed += OnChanged; // 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}"); }

在更改事件中,您可以重新加载文件和值。 

这是Njupiter现在不推荐使用的原因。

© www.soinside.com 2019 - 2024. All rights reserved.