Reputation: 115
I have a console application written in C# which uses an app.config file. This application is intended to be run on a server using the task scheduler. Now I want to develop a UI that reads and writes from and to the app.config. (Note that this config is not intended to replace the config file of the UI application.)
But I'm struggling to read the settings from the file. Using the ConfigurationManager I'm able to open the config file, BUT I cannot access the configuration settings.
This is the sample config file generated 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 consulting several article on stackoverflow, I tried this 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 as well:
var userSection = config.GetSection("AccessingConfigSample.Properties.Settings");
Both returned null.
So what am I doing wrong here?
Any help or hints are highly appreciated!
Upvotes: 5
Views: 4053
Reputation: 42414
The config file you use as an example is using a ConfigurationSectionGroup and those need to be read with the matching method GetSectionGroup on the Configuration element instead of GetSection
The following code snippet does output the content of the SectionGroup to the Debug console:
if (File.Exists(configFile))
{
var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
// get the sectionGroup!
var userSectionGroup = config.GetSectionGroup("userSettings");
foreach (var userSection in userSectionGroup.Sections)
{
// check for a ClientSettingSection
if (userSection is ClientSettingsSection)
{
// cast from ConfigSection to a more specialized type
var clientSettingSect = (ClientSettingsSection) userSection;
foreach (SettingElement clientSetting in clientSettingSect.Settings)
{
Debug.WriteLine(String.Format("{0}={1}", clientSetting.Name, clientSetting.Value.ValueXml.InnerText ));
}
}
}
}
Notice that I cast the object instance to a ClientSettingSection to retrieve the settings value (which is a SettingElement).
If you put this to work with the sample config you provided the result in the Debug Output Window pane should be:
ApplicationTitle=Accessing Config files
VersionNo=V 1.0
Upvotes: 5