【发布时间】:2011-08-10 19:18:02
【问题描述】:
我有以下问题:我正在向应用程序中引入新功能(作为 Windows 服务运行),我希望使用某种配置文件条目(myKey)。我可以在 app.config 中存储配置条目,但如果我想将其从 on->off 更改,否则需要重新启动 Windows 服务,我想避免它。我希望我的应用程序能够运行并获取配置更改。
问题是:.NET 中是否存在解决该问题的内置机制?我想我可以创建自己的配置文件,然后使用FileSystemWatcher 等...但也许.NET 允许使用外部配置文件并重新加载值??
ConfigurationManager.AppSettings["myKey"]
谢谢,帕维尔
编辑 1:感谢您的回复。但是我写了以下 sn-p 并且它不起作用(我尝试在两个地方创建 appSettingSection:循环前和循环内):
static void Main(string[] args)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("appSettings");
for (int i = 0; i < 10; i++)
{
ConfigurationManager.RefreshSection("appSettings");
AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("appSettings");
string myConfigData = appSettingSection.Settings["myConfigData"].Value; // still the same value, doesn't get updated
Console.WriteLine();
Console.WriteLine("Using GetSection(string).");
Console.WriteLine("AppSettings section:");
Console.WriteLine(
appSettingSection.SectionInformation.GetRawXml()); // also XML is still the same
Console.ReadLine();
}
}
当应用程序在 Console.ReadLine() 上停止时,我手动编辑配置文件。
【问题讨论】:
标签: c# .net app-config configuration-files