我有一个自定义设置文件( Settings.xml ),我在其中指定了我需要的所有内容。当运行网站时更改该文件,当然,重新启动。
将 Settings.xml 读入与xml结构相同的对象,然后使用Settings
对象
private static readonly XDocument Settings = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "\\Settings.xml");
该站点在IIS 8.5中运行。
是否可以在不强制网站重启的情况下更新 Settings.xml ?
是自动重启的IIS吗?
答案 0 :(得分:2)
您可以在每个文件更改中读取您的设置,如下所示:
private static DateTime? _lastSettingRead;
private static XDocument _cahedSettings;
private static XDocument Settings
{
get
{
var settingsPath = AppDomain.CurrentDomain.BaseDirectory + "\\Settings.xml";
//Get last change Settings file datetime
var lastSettingChange = System.IO.File.GetLastWriteTime(settingsPath);
//If we read first time or file changed since last read
if (!_lastSettingRead.HasValue || lastSettingChange > _lastSettingRead)
{
_lastSettingRead = lastSettingChange; //change read date
_cahedSettings = XDocument.Load(settingsPath); //load settings to field
}
return _cahedSettings; //return cached settings from field
}
}