我在我的configSection中创建了自定义app.config。由于某些原因,我无法将ForceSave设置为true:
MyCustomConfigSection.SectionInformation.ForceSave = true;我收到一个例外:
InvalidOperationException未被处理;此操作不适用于运行时。
我还需要为这个做点什么吗?
发布于 2011-11-03 15:13:54
@Jason,我刚才给你举了个例子。可能是别的什么。我解决了我的问题。为了保存自定义部分中的更改,我们需要从Configuration类中获取它。这是代码:
ConfigurationManager configFileManager = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MyCustomSection myCustomSection = (MyCustomSection )_configFileManager.GetSection("MyCustomSectionName");
//here we apply changes to our custom section
//and finally save our config file
configFileManager.ConfigFileManager.Save(ConfigurationSaveMode.Full);就这样。
发布于 2011-11-03 14:19:35
自定义配置对象在应用程序启动时初始化,并在应用程序运行时被读取。
通过设置配置类变量,您实际上是在试图写入.config文件,这会引发大量问题。这就是为什么它是一个不受支持的操作。
为什么要在运行时编写配置值?
https://stackoverflow.com/questions/7996182
复制相似问题