Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

section type in app.config

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?

like image 345
Silver Avatar asked Apr 18 '14 19:04

Silver


1 Answers

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"];
like image 68
texel Avatar answered Oct 07 '22 13:10

texel