.NET配置类保存问题

Vaj*_*jda 4 .net c# app-config

当我更改一些配置属性并调用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;
Run Code Online (Sandbox Code Playgroud)

我已将属性Config注入我的部分,因此我可以使用它来保存这样的配置.

mySection.Config.Save();
Run Code Online (Sandbox Code Playgroud)

Teo*_*gul 6

该文档说明:"如果自创建此Configuration对象后配置文件已更改,则会发生运行时错误." 因此,之后Save(),您需要销毁配置对象并重新创建它以使多个保存操作正常工作.