如何通过ConfigurationManager写入User.Config文件?
我正在尝试使用 ConfigurationManager 将用户设置保存到配置文件中。
我只想将这些设置的范围限制为用户,因为没有管理员权限就无法在 Vista/Win 7 上保存应用程序更改。
这似乎让我得到了用户的配置,该配置似乎保存在 Win 7 中 ([Drive]:\Users\[Username]\AppData\Local\[ApplicationName]\[AssemblyName][hash]\[Version\
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
)我尝试保存对此配置的任何更改,但出现此异常:
InnerException: System.InvalidOperationException
Message="ConfigurationSection properties cannot be edited when locked."
Source="System.Configuration"
StackTrace:
at System.Configuration.SectionInformation.VerifyIsEditable()
at System.Configuration.MgmtConfigurationRecord.GetConfigDefinitionUpdates(Boolean requireUpdates, ConfigurationSaveMode saveMode, Boolean forceSaveAll, ConfigDefinitionUpdates& definitionUpdates, ArrayList& configSourceUpdates)
我尝试向此配置添加自定义 ConfigurationSection。我尝试添加到 AppSettingsSection。每当我调用 config.Save() 时,它都会抛出上面的异常。
有什么想法吗?
我尝试通过“项目”->“设置”设计器使用 ApplicationSettingsBase 类,但似乎您无法用它保存自定义类型。我想要类似的功能以及保存自定义类型的能力。
I'm trying to persist user settings to a configuration file using ConfigurationManager.
I want to scope these settings to the user only, because application changes can't be saved on Vista/Win 7 without admin privileges.
This seems to get me the user's configuration, which appears to be saved here in Win 7 ([Drive]:\Users\[Username]\AppData\Local\[ApplicationName]\[AssemblyName][hash]\[Version\)
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
Whenever I try to save any changes at all to this config I get this exception:
InnerException: System.InvalidOperationException
Message="ConfigurationSection properties cannot be edited when locked."
Source="System.Configuration"
StackTrace:
at System.Configuration.SectionInformation.VerifyIsEditable()
at System.Configuration.MgmtConfigurationRecord.GetConfigDefinitionUpdates(Boolean requireUpdates, ConfigurationSaveMode saveMode, Boolean forceSaveAll, ConfigDefinitionUpdates& definitionUpdates, ArrayList& configSourceUpdates)
I have tried adding a custom ConfigurationSection to this config. I have tried adding to the AppSettingsSection. Whenever I call config.Save()
it throws the exception above.
Any ideas?
I tried using the ApplicationSettingsBase class through the Project->Settings designer, but it doesn't appear that you can save custom types with this. I want similar functionality with the ability to save custom types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要设置该部分的SectionInformation.AllowExeDefinition 值:
默认值是ConfigurationAllowExeDefinition.MachineToApplication,它只允许将该部分放置在machine.config 和app.exe.config 上。
You need to set the SectionInformation.AllowExeDefinition value for the section:
The default value is ConfigurationAllowExeDefinition.MachineToApplication which allows only to place the section on machine.config and app.exe.config.