How to Write to a User.Config file through ConfigurationManager?

后端 未结 1 423
南笙
南笙 2020-12-09 19:10

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

相关标签:
1条回答
  • 2020-12-09 20:02

    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.

    0 讨论(0)
提交回复
热议问题