.NET配置类保存问题 [英] .NET Configuration Class save problem

查看:164
本文介绍了.NET配置类保存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我改变一些配置属性并调用保存()方法来配置对象,然后更改一些配置属性并调用保存()再次,我得到异常,因为配置发生了变化。

除了文本:配置文件已被其他程序更改

所以,如果用户在运行时改变了一些东西,确实保存比变化的东西重新做救我的应用程序将抛出异​​常。我说得对?

我不应该能够保存配置多次?

编辑:只要code样品

  ExeConfigurationFileMap地图=新ExeConfigurationFileMap();
            map.LocalUserConfigFilename = UserConfig的;
            map.RoamingUserConfigFilename = RoamingConfig;
            map.ExeConfigFilename = AppConfig的;

            System.Configuration.Configuration combinedConfigFile = ConfigurationManager.OpenMappedExeConfiguration(地图,ConfigurationUserLevel.PerUserRoamingAndLocal);

            AddinConfiguration combinedConfig =(AddinConfiguration)combinedConfigFile.GetSection(sectionName);
            combinedConfig.Config = combinedConfigFile;
            返回combinedConfig;
 

我已经注入属性配置到我的部分,所以我可以用它来保存的配置是这样的。

  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();

解决方案

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.

这篇关于.NET配置类保存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆