我已经在我的app.config中创建了自定义configSection。对于一些原因,我不能设置ForceSave为true:configSection.SectionInformation.ForceSave错误c#
MyCustomConfigSection.SectionInformation.ForceSave = true;
我收到异常:
InvalidOperationException异常是未处理的;此操作不适用于运行时。
我需要为此做些更多的事吗?
我已经在我的app.config中创建了自定义configSection。对于一些原因,我不能设置ForceSave为true:configSection.SectionInformation.ForceSave错误c#
MyCustomConfigSection.SectionInformation.ForceSave = true;
我收到异常:
InvalidOperationException异常是未处理的;此操作不适用于运行时。
我需要为此做些更多的事吗?
@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);
就是这样。
自定义配置对象在应用程序启动时被初始化,并且应该在应用程序运行时被读取。
通过设置配置类变量,你基本上试图写入.config文件,这引发了一系列问题。这就是为什么它不受支持的操作。
你为什么要在运行时写配置值?
我想在运行时更改我的应用程序设置。例如,将connectionString更改为DataBase,无需重新加载应用程序。 –
http://msdn.microsoft.com/en-us/library/ms134088.aspx 这里是这种方法的一个例子 –
我不知道你为什么让你的应用程序用户连接到任意数据库。通常这是您从用户隐藏的类型。这是一个Windows应用程序或网络应用程序?如果它是一个Windows应用程序,可能会在应用程序退出而不是在运行时调用保存。我认为你需要重新检查你为什么要向用户公开这个功能。 – Jason