ALT user
ALT user

Reputation: 59

ConfigurationManager class. ConfigurationSection properties cannot be edited when locked

Here is code:

// These is works
Console.WriteLine(Properties.Settings.Default.name);

Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
KeyValueConfigurationCollection settings = configFile.AppSettings.Settings;
settings.Add("Port", "12");
// Here it fails
configFile.Save(ConfigurationSaveMode.Modified);

Content of app.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="ConsoleApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
        </sectionGroup>
    </configSections>
    <userSettings>
        <ConsoleApplication1.Properties.Settings>
            <setting name="name" serializeAs="String">
                <value>dasdqweqw</value>
            </setting>
        </ConsoleApplication1.Properties.Settings>
    </userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/></startup></configuration>

Can not understand why it is not working. Code is right or I am doing something wrong? On the line configFile.Save(ConfigurationSaveMode.Modified); it throws an exception "ConfigurationSection properties cannot be edited when locked."

What scenario I have decided to follow: 1. Via Solution explorer I am opening project properties page (Solution explorer > context menu of project node > Properties), then via Settings tab in project properties page I am creating project's application settings (value of Scope parameter is User). This creates Properties class (located in Settings.Designer.cs file) in namespace of the project & corresponding class properties. https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2008/cftf714c%28v=vs.90%29 2. To create user specific user.config file I have to change added settings & call Properties.Settings.Default.Save() method. If I just call Properties.Settings.Default.Save() without changing settings first this does not create user.config file.

Usage exmple:

Properties.Settings.Default.updateTime = DateTime.UtcNow;
Properties.Settings.Default.Save();

Console.WriteLine((Properties.Settings.Default.updateTime - DateTime.UtcNow).Hours);
Console.ReadLine();

Content of user.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <coonfoo.Properties.Settings>
            <setting name="updateTime" serializeAs="String">
                <value>06/03/2018 10:42:03</value>
            </setting>
        </coonfoo.Properties.Settings>
    </userSettings>
</configuration>

Upvotes: 2

Views: 2067

Answers (1)

Nilesh
Nilesh

Reputation: 2691

This should work.

class Program
{
    static void Main(string[] args)
    {
        Configuration roaming = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        fileMap.ExeConfigFilename = roaming.FilePath;
        Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        KeyValueConfigurationCollection settings = configuration.AppSettings.Settings;
        settings.Add("Port", "12");
        configuration.Save(ConfigurationSaveMode.Modified);
    }
}

Found this which might help you.

Upvotes: 2

Related Questions