I have a app.config file while is built as per the below :-
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="key1" value="value1" />
<add key="key2" value="value2" />
<add key="key3" value="value3" />
<add key="key4" value="value4" />
.....
</appSettings>
</configuration>
This config contains 12 entries for different keys, but when I debug my code I find that ConfigurationManager.AppConfig only contains the first 4 keys. I have tried using the code to refresh the section before calling it but still only contains 4 keys (the first 4 that were built in the app as we developed) and anything added after key 4 will not be shown.
Any ideas what I am doing wrong?
EDIT
Sorry for the lack of detail, the actual app.config file looks like :-
?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Lic" value="123456" />
<add key="API" value="https://example.com/api/" />
<add key="User" value="User" />
<add key="Config" value="Config"/>
<add key="ConN0" value="ConN Value 0, additional details here" />
<add key="ConN1" value="ConN Value 1, additional details here" />
<add key="ConN2" value="ConN Value 2, additional details here" />
<add key="ConN3" value="ConN Value 3, additional details here" />
<add key="ConN4" value="ConN Value 4, additional details here" />
<add key="ConN5" value="ConN Value 5, additional details here" />
<add key="ConN6" value="ConN Value 6, additional details here" />
<add key="ConN7" value="ConN Value 7, additional details here" />
</appSettings>
</configuration>
The code I am trying to run is as follows :
List<string> values = new List<string>();
foreach (string key in ConfigurationManager.AppSettings)
{
if (key.StartsWith("ConN"))
{
string value = ConfigurationManager.AppSettings[key];
values.Add(value);
}
}
The code always produces an empty list, and when stepping through the ConfigurationManager.AppSettings always has only 4 entries for Lic, API, User and Config. We have narrowed down the location of the issue to the code above as the next step of the code is to display the key values in a datagrid but it always comes up empty even in a full build. We have checked the .config file in the build and it has the correct entries in it.
I am not sure where I should be looking for the configuration of Debug as I cannot find anywhere that references the app.config file location. I have searched the machine for other app.config files and the only relevant one I can find for this project is the one with the full config held in it.
If anyone could take pity on me and point me in the right direction I would be eternally grateful?
I haven't actually found out why I can only see 4 entries in the ConfigurationManager.AppSettings, but I have worked around the problem.
I moved the config values into their own section within the app.config using :
<configSections>
<section name="MyConfig" type="System.Configuration.AppSettingsSection" />
</configSections>
Then changed all the reference in the code to open a new AppSettingsSection like below :
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("MyConfig");
by doing this and then referencing the appSettingSection instead of ApplicationManager.AppSettings the keys and values are available and working.