读取检测文件更改的配置文件(xml 或 json)的库
Library to read config files (xml or json) that detects file´s changes
我有一个这样的 XML 配置文件:
<configuration>
<items>
<item key="KEY_NAME">
3
</item>
</items>
</configuration>
我需要一些库来读取(仅读取)文件值,但无需重新启动应用程序即可检测到更改(例如,将值 3 更改为 5)。
我正在使用 njupiter for that, but this project was deprecated. I found MiniFSWatcher,你知道其他图书馆吗?
谢谢,
为什么不用官方https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=netframework-4.8class看文件系统?
示例如下:
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 的原因。
我有一个这样的 XML 配置文件:
<configuration>
<items>
<item key="KEY_NAME">
3
</item>
</items>
</configuration>
我需要一些库来读取(仅读取)文件值,但无需重新启动应用程序即可检测到更改(例如,将值 3 更改为 5)。
我正在使用 njupiter for that, but this project was deprecated. I found MiniFSWatcher,你知道其他图书馆吗?
谢谢,
为什么不用官方https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=netframework-4.8class看文件系统?
示例如下:
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 的原因。