5

I was going through how to create section groups and sections in configsection of app.config. So, lets take a sample

    <configSections>
    <sectionGroup name="trackGroup">
    <section name="trackInfo" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </configSections>
    <trackGroup>
    <trackInfo>
    <add key = "ASHUM" value="ASH-UM"/>
    </trackInfo>
</trackGroup>

So now while I was going through various articles I found that everyone uses different types for section So, I found here the different types: http://msdn.microsoft.com/en-us/library/aa309408%28v=vs.71%29.aspx

But the type I am using is not mentioned here. I got this type from a random example and as far as I get it it is actually defining settings for appsetting section. Can someone help me what does the type means in my sample I mean what is version, public token,culture how we define these? Also I wanted to know which type is more better to be used? Like i have to access these settings during runtime and even modify some during runtime.

Also I suppose that these different types has different ways by which we can access them? Like in case above in my sample I am accessing the keys and values as:

  static void getFull(string sectionName)
        {
            System.Collections.Specialized.NameValueCollection section = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection(sectionName);
            if (section != null)
            {
                foreach (string key in section.AllKeys)
                {
                    Console.WriteLine(key + ": " + section[key]);
                }
            }
        }

But if we use types like those in the MSDN link provided how I am going to access the keys and values?

Silver
  • 443
  • 2
  • 12
  • 33

1 Answers1

6

This is something I do for creating config sections. It also allows externalizing sections.

App.config

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="TrackInfo" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </configSections>

    <TrackInfo configSource="TrackInfo.config"/>
</configuration>

TrackInfo.config

<?xml version="1.0" encoding="utf-8" ?>
<TrackInfo>
    <add key="SOME_KEY" value="SOME_VALUE" />
</TrackInfo>

C#

NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection( "TrackInfo" );
if( null == section ) throw new Exception( "Missing TrackInfo.config" );

string val = section["SOME_KEY"];
texel
  • 184
  • 9
  • Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> how it is decided? can i give any version any token ? – Silver Apr 19 '14 at 18:11
  • Not 100% sure, but I think it needs to match the .net framework. I've used 3.5.0.0 and 4.0.0.0 – texel Apr 24 '14 at 15:20
  • yeah I thought so too but when i used Version=3.5.0.0 for a project in 3.5 framework it gave me error..Version=2.0.0.0 works fine for all for me till now – Silver Apr 25 '14 at 01:46
  • 2
    Use 2.0.0.0 for frameworks 2.0 through 3.5.1. Use 4.0.0.0 for 4.x. The token is the token that represents the cryptographic information that was used to sign that assembly. It needs to be correct – Flydog57 Feb 19 '19 at 20:11
  • @Flydog57 when you say "that assembly" – would that be `System.Configuration` in the example with `
    ` ?
    – unhammer Aug 12 '19 at 19:11