I have a console application written in C # that uses the app.config file. This application is designed to run on the server using the task scheduler. Now I want to develop a user interface that reads and writes using and app.config. (Note that this configuration is not intended to replace the UI application configuration file.)
But I'm struggling to read the settings from the file. Using the ConfigurationManager, I can open the configuration file, BUT I cannot access the configuration settings.
This is an example configuration file created by Visual Studio (2010):
<?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="AccessingConfigSample.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<AccessingConfigSample.Properties.Settings>
<setting name="ApplicationTitle" serializeAs="String">
<value>Accessing Config files</value>
</setting>
<setting name="VersionNo" serializeAs="String">
<value>V 1.0</value>
</setting>
</AccessingConfigSample.Properties.Settings>
</userSettings>
</configuration>
After several articles on stackoverflow, I tried to open the file and access the user section:
if (File.Exists(configFile))
{
var configMap = new ExeConfigurationFileMap{ ExeConfigFilename = configFile};
var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
var userSection = config.GetSection("userSettings");
}
I tried this too:
var userSection = config.GetSection("AccessingConfigSample.Properties.Settings");
null.
, ?
!