Al Phaba
Al Phaba

Reputation: 6755

How to retrieve a value from the userSettings section in c#

I have this config file

<?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="DynamicFormWorker.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" />
  </startup>
  <userSettings>
    <DynamicFormWorker.Properties.Settings>
      <setting name="mandator" serializeAs="String">
        <value>$$mandator$$</value>
      </setting>
    </DynamicFormWorker.Properties.Settings>
  </userSettings>
  <appSettings>
    <add key="log4net.Config" value="log4net.config" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

Now I figured out how to load a concrete config file in c# but actually I don't get my value for the mandator element.

I am loading the exe config like this

        configLocation = new ExeConfigurationFileMap();            
        configLocation.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App.config");
        exeConfig = ConfigurationManager.OpenMappedExeConfiguration(configLocation, ConfigurationUserLevel.None);

But how do I retrieve the mandator element inside the userSettings? Thanks

Upvotes: 3

Views: 4081

Answers (1)

Stewart_R
Stewart_R

Reputation: 14485

I make no assertion that this is the 'standard' or 'accepted' way of reaching that value (it seems too long winded!) but you can do this:

ConfigurationSectionGroup userSettings = config.SectionGroups["userSettings"];
var settingsSection = userSettings.Sections["DynamicFormWorker.Properties.Settings"] as ClientSettingsSection;
string mandator = settingsSection.Settings.Get("mandator").Value.ValueXml.InnerText;

Upvotes: 3

Related Questions