Ale*_*nes 9 .net c# app-config config
我想读取app.config值,在消息框中显示它,使用外部文本编辑器更改值,最后显示更新的值.
我尝试使用以下代码:
private void button2_Click(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationManager.RefreshSection("appSettings");
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
MessageBox.Show(ConfigurationManager.AppSettings["TheValue"]);
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.它显示旧值(在外部文本编辑器中更改之前).有什么建议?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="TheValue" value="abc"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
Sac*_*hin 10
它可能会帮助你
尝试保存这样的配置
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["KeyName"].Value = "NewValue";
config.AppSettings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
Run Code Online (Sandbox Code Playgroud)
然后像这样获取它
ConfigurationManager.RefreshSection("appSettings");
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Run Code Online (Sandbox Code Playgroud)
小智 3
您可以尝试使用以下代码:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = config.AppSettings.Settings;
// update SaveBeforeExit
settings["TheValue"].Value = "WXYZ";
config.Save(ConfigurationSaveMode.Modified);
MessageBox.Show(ConfigurationManager.AppSettings["TheValue"]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16100 次 |
| 最近记录: |