How to get value from 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 specific config file in C #, but I am not actually getting a value for the mandator element.

I am loading exe config as follows

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

      

But how can I get the mandator element inside userSettings? Thanks to

+3


source to share


1 answer


I am not claiming that this is the "standard" or "accepted" way to achieve this value (seems like too long wind!), But you can do it:



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

      

+1


source







All Articles