c# - App.Config 未更新

标签 c# winforms app-config

我想更新应用程序。 windows 窗体 c# 中的配置文件。这是更新应用程序的代码。配置

 // updating
 System.Configuration.Configuration config =  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 config.AppSettings.Settings.Remove("name");
 config.AppSettings.Settings.Add("name",txtName.Text);
 config.Save(ConfigurationSaveMode.Modified);
 ConfigurationManager.RefreshSection("appSettings");

 // show updating result on labels ( and this work fine )
 string value = ConfigurationManager.AppSettings["name"];
 lblName.Text = value;

当我运行一个应用程序时,这个更新工作正常,但是当我重新启动应用程序时,所有配置都重置为默认值

最佳答案

不使用配置类,而是将文件作为常规 Xml 文件打开并进行更改。请注意,当您执行此操作时,当您将文件保存回来时,.config 文件中的任何注释都将被 Xml 类删除。为了防止这种情况,您必须从原始配置文件中读取所有类型的 Xml 节点并将它们全部写回。

关于c# - App.Config 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24523171/

相关文章:

c# - 冒泡事件的首选方式是什么?

c# - 在 mvc 应用程序中发送电子邮件时遇到问题

.net - 在.net中管理标签

.net - 从流而不是文件加载配置文件

WCF 服务 : app. 配置与属性或两者的混合

C# 代码契约(Contract) : Are postconditions on members accessible from other threads useless?

c# - Entity Framework 6 代码优先触发器

.net - 您能否为 .NET Winforms 应用程序推荐低成本的自动化测试工具?

c# - 如何使用鼠标按住和释放检查 TrackBar 是否滑动

.net - 存储 .NET DLL(不是 EXE 或 Web)的配置设置