繁体   English   中英

如何在运行时修改AppSettings键值?

[英]How to Modify AppSettings key value at run time?

        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        config.AppSettings.Settings["SourceFilePath"].Value = SourcePathTextBox.Text.ToString();
        config.AppSettings.SectionInformation.ForceSave = true;
        config.Save(ConfigurationSaveMode.Modified, true);
        ConfigurationManager.RefreshSection("appSettings");

这是我更改键设置值的代码。 它正在正常工作,但没有错误,但值在app.config文件中的应用程序设置中未更改。请帮助我。

每次重新App.Config解决方案时, App.Config文件的值都会还原。 只有在部署应用程序时,它才能按需工作。

或者你可以

将密钥添加到Settings.settings如下所示。

在此处输入图片说明

设定值

像这样写

ModelCodeGenerator.Properties.Settings.Default.DefaultTargetPath = txtFolder.Text;
ModelCodeGenerator.Properties.Settings.Default.Save();

ModelCodeGenerator是我的解决方案名称。

您提供的代码不会持久保存对配置文件的更改,而只是在内存中进行更改。 您可以使用断点检查更改,也可以使用以下代码显示更新的值:。

lblConfigChanges.Text=System.Configuration.ConfigurationManager.AppSettings["SourceFilePath"].ToString();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM