12

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.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Josh G
  • 14,068
  • 7
  • 62
  • 74
  • Turns out you CAN use custom to types with the Project->Settings designer. They don't list them in the type dialog, but if you type in the right name/namespace you can use any type you want. The type should be XmlSerializable or convertable to/from string with a TypeConverter. – Josh G May 11 '11 at 16:48

1 Answers1

8

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.

Adam Kalnas
  • 1,188
  • 2
  • 11
  • 25
Jay
  • 56,361
  • 10
  • 99
  • 123
  • I'll try that. I tried setting the AllowExeDefinition property once and caused the same exception that config.Save() fired off. I'll copy this code segment and try again. – Josh G Apr 13 '10 at 20:19
  • It appears I needed to set the AllowExeDefinition BEFORE adding the section and saving the changes for the first time. Now everything appears to be working fine. – Josh G Apr 13 '10 at 20:29
  • 2
    for some reason, using the _ConfigurationManager.OpenExeConfiguration_ with _ConfigurationUserLevel_ overload, didn't allowed me to edit & save the file by claiming that _ConfigurationSection properties cannot be edited when locked_. only when I've used the _string exepath_ overload, I was capable of saving the configuration. – itsho Jan 06 '13 at 20:50
  • I just encountered this. I fixed it by updating the new section before adding it to the config.Sections collection. – Darrel Lee Jul 18 '16 at 10:19
  • where is UserSettings ? in framework 4.8? – KillemAll Jul 04 '23 at 12:19