如何通过ConfigurationManager写入User.Config文件?

发布于 2024-08-28 13:41:53 字数 1123 浏览 11 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2024-09-04 13:41:53

您需要设置该部分的SectionInformation.AllowExeDefinition 值:

 Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
UserSettings settings;
if ((settings = (UserSettings)configuration.Sections[GENERAL_USER_SETTINGS]) == null)
{
      settings = new UserSettings();
      settings.SectionInformation.AllowExeDefinition =   
                 ConfigurationAllowExeDefinition.MachineToLocalUser;
      configuration.Sections.Add(GENERAL_USER_SETTINGS, settings);
      configuration.Save();
}

默认值是ConfigurationAllowExeDefinition.MachineToApplication,它只允许将该部分放置在machine.config 和app.exe.config 上。

You need to set the SectionInformation.AllowExeDefinition value for the section:

 Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
UserSettings settings;
if ((settings = (UserSettings)configuration.Sections[GENERAL_USER_SETTINGS]) == null)
{
      settings = new UserSettings();
      settings.SectionInformation.AllowExeDefinition =   
                 ConfigurationAllowExeDefinition.MachineToLocalUser;
      configuration.Sections.Add(GENERAL_USER_SETTINGS, settings);
      configuration.Save();
}

The default value is ConfigurationAllowExeDefinition.MachineToApplication which allows only to place the section on machine.config and app.exe.config.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文