.NET配置类保存问题
当我更改某些配置属性并调用配置对象的 Save() 方法,然后更改某些配置属性并再次调用 Save() 时,我会收到异常,因为配置已更改。
异常文本:“配置文件已被另一个程序更改。”
因此,如果用户在运行时更改某些内容并保存,然后再次更改某些内容并保存,我的应用程序将引发异常。我说得对吗?
我不应该能够多次保存配置吗?
编辑:提供了代码示例。
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.LocalUserConfigFilename = UserConfig;
map.RoamingUserConfigFilename = RoamingConfig;
map.ExeConfigFilename = AppConfig;
System.Configuration.Configuration combinedConfigFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoamingAndLocal);
AddinConfiguration combinedConfig = (AddinConfiguration)combinedConfigFile.GetSection(sectionName);
combinedConfig.Config = combinedConfigFile;
return combinedConfig;
我已将属性 Config 注入到我的部分中,以便我可以使用它来保存这样的配置。
mySection.Config.Save();
When I change some config property and call Save() method to configuration object, and then change some config property and call Save() again I get exception because config was changed.
Text of exception: "The configuration file has been changed by another program."
So if user in run time changes something and do save and than changes something again and do save my app will throw exception. Am I right?
Shouldn't I be able to save config multiple times?
EDIT: Provided code sample.
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.LocalUserConfigFilename = UserConfig;
map.RoamingUserConfigFilename = RoamingConfig;
map.ExeConfigFilename = AppConfig;
System.Configuration.Configuration combinedConfigFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoamingAndLocal);
AddinConfiguration combinedConfig = (AddinConfiguration)combinedConfigFile.GetSection(sectionName);
combinedConfig.Config = combinedConfigFile;
return combinedConfig;
I have injected property Config into my section so I can use it to save configuration like this.
mySection.Config.Save();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档指出:“如果配置文件自创建此配置对象以来发生了更改,则会发生运行时错误。”因此,在
Save()
之后,您需要销毁配置对象并重新创建它,以便多个保存操作能够正常工作。The documentation states: "If the configuration file has changed since this Configuration object was created, a run-time error occurs." So after
Save()
, you need to destroy the configuration object and recreate it for multiple save operations to work normally.