0

i'm working on an app that allow users to manage windows programs.

So i have a .exe.config file and contains this:

<?xml version="1.0" encoding="utf-8" ?><configuration>
<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="Control_Desk.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<userSettings>
    <Control_Desk.Properties.Settings>          
        <setting name="enabletw" serializeAs="String">
            <value>True</value>
        </setting>
        <setting name="enablelivemail" serializeAs="String">
            <value>True</value>
        </setting>
        <setting name="enabledesktop" serializeAs="String">
            <value>True</value>
        </setting>
        <setting name="enablelogmein" serializeAs="String">
            <value>True</value>
        </setting>
        <setting name="enableps" serializeAs="String">
            <value>False</value>
        </setting>
        <setting name="enablecorel" serializeAs="String">
            <value>False</value>
        </setting>
        <setting name="enablescanner" serializeAs="String">
            <value>False</value>
        </setting>
    </Control_Desk.Properties.Settings>
</userSettings>

And i want to read and change the valeus, but, no sucess, no changes are saved and i cant read any value, when i try, the program throws a System.NullReferenceException, here is my code:

        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        if (config.AppSettings.Settings["enablelivemail"].Value == "True")
            {
                config.AppSettings.Settings["enablelivemail"].Value = "False";
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
                //ConfigurationManager.RefreshSection("appSettings");
            }
            else
            {
                config.AppSettings.Settings["enablelivemail"].Value = "True";
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
            }

So, on second line i got the exception and nothing works, any help?

Thanks in advance!

Bazz
  • 35
  • 1
  • 9

1 Answers1

1

config.AppSettings on 2nd line reads from <appSettings> node in config file. And there is no <appSettings> node on your config file.

Check this link to solve your problem.

Community
  • 1
  • 1
Onur Gazioğlu
  • 501
  • 2
  • 12
  • You need to begin explaining the solution, where a separate Section Group and Section are created, since `userSettings` is not a standard group and `Control_Desk.Properties.Settings`, not a standard section – Mrinal Kamboj Oct 13 '16 at 05:44