0

I am absolutely at my wits end with this, having tried basically everything. I also do not see any existing stackoverflow threads about doing this.

I have an app.config file for my C# project, and it stores a list of servers which the user can create and add to.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="servers" type="System.Configuration.AppSettingsSection" />
    </configSections>
    <servers>
        <add key="server" value="678,true,true"/>
    </servers>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
    </startup>
    <appSettings>
        <add key="nightmode" value="Dark"/>
        <add key="theme" value="Red"/>
    </appSettings>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="ControlzEx" publicKeyToken="69f1c32f803d307e" culture="neutral"/>
                <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

((NameValueCollection)ConfigurationManager.GetSection("servers")).Add("server", $"{port}," + $"{ (dialogResult1 == MessageDialogResult.Affirmative) },{dialogResult2 == MessageDialogResult.Affirmative}");

When the user goes to add a new key the the "servers" section, it throws the below exception.

System.Configuration.ConfigurationErrorsException: 'The configuration is read only.'

I am bewildered why this is happening

Meme Machine
  • 949
  • 3
  • 14
  • 28
  • Does this answer your question? [Why are application settings read-only in app.config?](https://stackoverflow.com/questions/758389/why-are-application-settings-read-only-in-app-config) – devlin carnate Dec 15 '21 at 19:27
  • It's because your app config is read only. As the error message says. https://social.technet.microsoft.com/wiki/contents/articles/30915.c-local-files.aspx – Andy Dec 15 '21 at 19:42
  • Why are you trying to write to the config file at runtime...? – mm8 Dec 15 '21 at 19:49
  • @mm8 Well what do you propose? I need some wait to save the settings in a configuration file – Meme Machine Dec 15 '21 at 19:51
  • @devlincarnate Might provide the reason, but I'm unsure what is a proper fix for this issue. – Meme Machine Dec 15 '21 at 19:53
  • The configuration file is not the place to save dynamic application specific settings I am afraid. – mm8 Dec 15 '21 at 19:53
  • @mm8 Alright. Would you recommend generating another XML file? Since the app.config is apparently completely useless for anything. – Meme Machine Dec 15 '21 at 19:54
  • Try using a `.settings` file. The `.config` file is certainly not useless unless you indend to edit it at runtime which doesn't make much sense. – mm8 Dec 15 '21 at 19:55
  • If you read the thread I linked above, the answers discuss why you can't write to app.config and they offer solutions for how you should handle the scenario where you want to write to a config. All of the reasoning is there, in the answers. Happy Reading! – devlin carnate Dec 15 '21 at 20:09

2 Answers2

-2

you can try:

Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings.Add("test", "propertyValue");

config.Save(ConfigurationSaveMode.Modified, true);
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");

string test = ConfigurationManager.AppSettings["test"];

This get de appsettings section values.

Ps.: You need Install-Package System.Configuration.ConfigurationManager.

Diego Lobo
  • 156
  • 7
-2

I ended up generating my own XML configuration, since that appears to be the best way to do things.

Meme Machine
  • 949
  • 3
  • 14
  • 28