我有这样的XML配置文件:
<configuration>
<items>
<item key="KEY_NAME">
3
</item>
</items>
</configuration>
我需要一些库来读取(仅读取)文件的值,但无需重新启动应用程序就可以检测到更改(例如,将值3更改为5)。我当时使用njupiter,但该项目已弃用。我找到了MiniFSWatcher,您还认识其他图书馆吗?
谢谢,
以下示例:
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现在不推荐使用的原因。