3

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

neontapir
  • 4,698
  • 3
  • 37
  • 52
Al Phaba
  • 6,545
  • 12
  • 51
  • 83
  • Although this question talks about writing to userSettings, it might still be of interest: http://stackoverflow.com/questions/629143/how-to-write-to-the-main-exes-config-usersettings-section – neontapir May 11 '15 at 16:19

1 Answers1

3

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;
Stewart_R
  • 13,764
  • 11
  • 60
  • 106
  • I unfortunately came to the same conclusion, as I could not find any alternative way. I think this is the way when you need to access the user settings through `ConfigurationManager`. – Tarik Sep 09 '16 at 19:53