<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Custom Configuration Section in C#</title>
	<atom:link href="http://nnish.com/2009/09/17/custom-configuration-section-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://nnish.com/2009/09/17/custom-configuration-section-in-c/</link>
	<description>My Passions Live Here!</description>
	<lastBuildDate>Tue, 25 Oct 2011 02:15:33 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: nnish</title>
		<link>http://nnish.com/2009/09/17/custom-configuration-section-in-c/#comment-95</link>
		<dc:creator><![CDATA[nnish]]></dc:creator>
		<pubDate>Sat, 25 Sep 2010 18:20:18 +0000</pubDate>
		<guid isPermaLink="false">http://nnish.wordpress.com/2009/09/17/custom-configuration-section-in-c/#comment-95</guid>
		<description><![CDATA[Hi ash.. 

You can do this, change the MessageElementCollection to KeyValueConfigurationCollection, and add the type as KeyValueConfigurationElement instead of MessageElement
   [ConfigurationCollection(typeof(KeyValueConfigurationElement), AddItemName = &quot;Message&quot;,
         CollectionType = ConfigurationElementCollectionType.BasicMap)]

Now in the configuration (messages element) change the &quot;name&quot;, &quot;value&quot; to &quot;key&quot; &quot;value&quot; (this is not necessary if you can explicitly provide it)

Sample:

      
      


Now use this code to add a new key value to the config section:

 // Open App.Config of executable
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            
            ((MyConfiguration)config.Sections[&quot;MyConfiguration&quot;]).Messages.Add(&quot;Tweet&quot;, &quot;Tweet&quot;);
            // Save the configuration file.
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of a changed section.
            ConfigurationManager.RefreshSection(&quot;MyConfiguration&quot;);
            MyConfig = GetConfiguration();

            Console.WriteLine(&quot;Message Tweet: &quot; + MyConfig.Messages[&quot;Tweet&quot;].Value);

Similarly for removing use Remove() method instead of Add()

Let me know if it works for you...]]></description>
		<content:encoded><![CDATA[<p>Hi ash.. </p>
<p>You can do this, change the MessageElementCollection to KeyValueConfigurationCollection, and add the type as KeyValueConfigurationElement instead of MessageElement<br />
   [ConfigurationCollection(typeof(KeyValueConfigurationElement), AddItemName = "Message",<br />
         CollectionType = ConfigurationElementCollectionType.BasicMap)]</p>
<p>Now in the configuration (messages element) change the &#8220;name&#8221;, &#8220;value&#8221; to &#8220;key&#8221; &#8220;value&#8221; (this is not necessary if you can explicitly provide it)</p>
<p>Sample:</p>
<p>Now use this code to add a new key value to the config section:</p>
<p> // Open App.Config of executable<br />
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);</p>
<p>            ((MyConfiguration)config.Sections["MyConfiguration"]).Messages.Add(&#8220;Tweet&#8221;, &#8220;Tweet&#8221;);<br />
            // Save the configuration file.<br />
            config.Save(ConfigurationSaveMode.Modified);<br />
            // Force a reload of a changed section.<br />
            ConfigurationManager.RefreshSection(&#8220;MyConfiguration&#8221;);<br />
            MyConfig = GetConfiguration();</p>
<p>            Console.WriteLine(&#8220;Message Tweet: &#8221; + MyConfig.Messages["Tweet"].Value);</p>
<p>Similarly for removing use Remove() method instead of Add()</p>
<p>Let me know if it works for you&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ash</title>
		<link>http://nnish.com/2009/09/17/custom-configuration-section-in-c/#comment-93</link>
		<dc:creator><![CDATA[ash]]></dc:creator>
		<pubDate>Thu, 23 Sep 2010 16:45:29 +0000</pubDate>
		<guid isPermaLink="false">http://nnish.wordpress.com/2009/09/17/custom-configuration-section-in-c/#comment-93</guid>
		<description><![CDATA[Hi. Thank you for this tutorial! I have it all working but was wondering how can you add/save a message(eg: create a new  in the config file) and also remove/delete a message during program execution? Thanks.]]></description>
		<content:encoded><![CDATA[<p>Hi. Thank you for this tutorial! I have it all working but was wondering how can you add/save a message(eg: create a new  in the config file) and also remove/delete a message during program execution? Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alex</title>
		<link>http://nnish.com/2009/09/17/custom-configuration-section-in-c/#comment-83</link>
		<dc:creator><![CDATA[alex]]></dc:creator>
		<pubDate>Fri, 11 Jun 2010 19:58:46 +0000</pubDate>
		<guid isPermaLink="false">http://nnish.wordpress.com/2009/09/17/custom-configuration-section-in-c/#comment-83</guid>
		<description><![CDATA[here what I do:


  
    &lt;!--Config section Group and Sections Declaration--&gt;
    
      
      
      
      
    
    
      
      
      
      
    
  

  &lt;!--Config Sections Declaration--&gt;
  
    
      
      
    
    
      
      
    
    
      
      
    
    
      
      
    
  
  
    
      
      
    
    
      
      
    
    
      
      
    
    
      
      
    
  
  
    &lt;!-- Which connection string to use from the connectionStrings section. --&gt;
    
    
    
  

-------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Configuration;
using System.Collections.Specialized;


namespace tryConfigApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // reading AppSettings---------------
            int pingIntervalMSec = Convert.ToInt32(ConfigurationManager.AppSettings[&quot;pingIntervalMSec&quot;].ToString());
            Console.WriteLine(&quot;pingIntervalMSec: {0}&quot;, pingIntervalMSec.ToString());
            //-------------------
            // reading specific custom section in custom group--------------
            NameValueCollection serverSection = (NameValueCollection)ConfigurationManager.GetSection(@&quot;Servers/mrprodapp01&quot;);
            if (serverSection != null)
            {
                foreach (string key in serverSection)
                {
                    Console.WriteLine(&quot;Section Key: {0}, {1}&quot;, key, serverSection[key]);
                }
            }
            //-----------------------------------------

           

           // --- get specific custom group
            System.Configuration.Configuration configBB = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConfigurationSectionGroup serversGroup = configBB.SectionGroups.Get(&quot;Servers&quot;);
          
            Console.WriteLine(&quot;Key value: {0}&quot;, serversGroup.Sections.Count.ToString() );
           
            for(int cnt=0; cnt &lt; serversGroup.Sections.Count; cnt++)
            {
                ConfigurationSection csTmp = serversGroup.Sections[cnt];
                serverSection = (NameValueCollection)ConfigurationManager.GetSection(&quot;Servers/&quot; + csTmp.SectionInformation.Name);
                 if (serverSection != null)
                {
                    foreach (string key in serverSection)
                    {
                        Console.WriteLine(&quot;Section Key: Servers/{0} {1}, {2}&quot;, csTmp.SectionInformation.Name, key, serverSection[key]);
                    }
                }
            }

            Console.WriteLine(&quot;-------------- LOOPING ALL ---------------&quot;);
              //--------- loop over all custom groups ------------------------------
            
 
            ConfigurationSectionGroupCollection groups =   configBB.SectionGroups;
            foreach (System.Configuration.ConfigurationSectionGroup grr in groups)
            {

                if (grr.Name.StartsWith(&quot;System.&quot;, StringComparison.OrdinalIgnoreCase)) continue;
                // if (!grr.IsDeclared) 
                //     continue;
     
                ConfigurationSectionGroup serversGroupA = configBB.SectionGroups.Get(grr.Name);

             

                for (int cnt = 0; cnt &lt; serversGroupA.Sections.Count; cnt++)
                {
                    ConfigurationSection csTmp = serversGroup.Sections[cnt];
                    serverSection = (NameValueCollection)ConfigurationManager.GetSection(grr.Name + &quot;/&quot; + csTmp.SectionInformation.Name);
                    if (serverSection != null)
                    {
                        foreach (string key in serverSection)
                        {
                            Console.WriteLine(&quot;Section Key: {0} {1}, {2}&quot;, grr.Name + &quot;/&quot; +csTmp.SectionInformation.Name, key, serverSection[key]);
                        }
                    }
                }


            }


      

        }
    }
}]]></description>
		<content:encoded><![CDATA[<p>here what I do:</p>
<p>    <!--Config section Group and Sections Declaration--></p>
<p>  <!--Config Sections Declaration--></p>
<p>    <!-- Which connection string to use from the connectionStrings section. --></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Diagnostics;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Threading;<br />
using System.Configuration;<br />
using System.Collections.Specialized;</p>
<p>namespace tryConfigApp<br />
{<br />
    class Program<br />
    {<br />
        static void Main(string[] args)<br />
        {<br />
            // reading AppSettings&#8212;&#8212;&#8212;&#8212;&#8212;<br />
            int pingIntervalMSec = Convert.ToInt32(ConfigurationManager.AppSettings["pingIntervalMSec"].ToString());<br />
            Console.WriteLine(&#8220;pingIntervalMSec: {0}&#8221;, pingIntervalMSec.ToString());<br />
            //&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
            // reading specific custom section in custom group&#8212;&#8212;&#8212;&#8212;&#8211;<br />
            NameValueCollection serverSection = (NameValueCollection)ConfigurationManager.GetSection(@&#8221;Servers/mrprodapp01&#8243;);<br />
            if (serverSection != null)<br />
            {<br />
                foreach (string key in serverSection)<br />
                {<br />
                    Console.WriteLine(&#8220;Section Key: {0}, {1}&#8221;, key, serverSection[key]);<br />
                }<br />
            }<br />
            //&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>           // &#8212; get specific custom group<br />
            System.Configuration.Configuration configBB = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);<br />
            ConfigurationSectionGroup serversGroup = configBB.SectionGroups.Get(&#8220;Servers&#8221;);</p>
<p>            Console.WriteLine(&#8220;Key value: {0}&#8221;, serversGroup.Sections.Count.ToString() );</p>
<p>            for(int cnt=0; cnt &lt; serversGroup.Sections.Count; cnt++)<br />
            {<br />
                ConfigurationSection csTmp = serversGroup.Sections[cnt];<br />
                serverSection = (NameValueCollection)ConfigurationManager.GetSection(&quot;Servers/&quot; + csTmp.SectionInformation.Name);<br />
                 if (serverSection != null)<br />
                {<br />
                    foreach (string key in serverSection)<br />
                    {<br />
                        Console.WriteLine(&quot;Section Key: Servers/{0} {1}, {2}&quot;, csTmp.SectionInformation.Name, key, serverSection[key]);<br />
                    }<br />
                }<br />
            }</p>
<p>            Console.WriteLine(&quot;&#8212;&#8212;&#8212;&#8212;&#8211; LOOPING ALL &#8212;&#8212;&#8212;&#8212;&#8212;&quot;);<br />
              //&#8212;&#8212;&#8212; loop over all custom groups &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>            ConfigurationSectionGroupCollection groups =   configBB.SectionGroups;<br />
            foreach (System.Configuration.ConfigurationSectionGroup grr in groups)<br />
            {</p>
<p>                if (grr.Name.StartsWith(&quot;System.&quot;, StringComparison.OrdinalIgnoreCase)) continue;<br />
                // if (!grr.IsDeclared)<br />
                //     continue;</p>
<p>                ConfigurationSectionGroup serversGroupA = configBB.SectionGroups.Get(grr.Name);</p>
<p>                for (int cnt = 0; cnt &lt; serversGroupA.Sections.Count; cnt++)<br />
                {<br />
                    ConfigurationSection csTmp = serversGroup.Sections[cnt];<br />
                    serverSection = (NameValueCollection)ConfigurationManager.GetSection(grr.Name + &quot;/&quot; + csTmp.SectionInformation.Name);<br />
                    if (serverSection != null)<br />
                    {<br />
                        foreach (string key in serverSection)<br />
                        {<br />
                            Console.WriteLine(&quot;Section Key: {0} {1}, {2}&quot;, grr.Name + &quot;/&quot; +csTmp.SectionInformation.Name, key, serverSection[key]);<br />
                        }<br />
                    }<br />
                }</p>
<p>            }</p>
<p>        }<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nnish</title>
		<link>http://nnish.com/2009/09/17/custom-configuration-section-in-c/#comment-82</link>
		<dc:creator><![CDATA[nnish]]></dc:creator>
		<pubDate>Fri, 21 May 2010 17:26:11 +0000</pubDate>
		<guid isPermaLink="false">http://nnish.wordpress.com/2009/09/17/custom-configuration-section-in-c/#comment-82</guid>
		<description><![CDATA[Glad that yours got fixed. :) You can download my sample and tweak it for your need. It works with VS 2010.]]></description>
		<content:encoded><![CDATA[<p>Glad that yours got fixed. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  You can download my sample and tweak it for your need. It works with VS 2010.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FJ</title>
		<link>http://nnish.com/2009/09/17/custom-configuration-section-in-c/#comment-81</link>
		<dc:creator><![CDATA[FJ]]></dc:creator>
		<pubDate>Fri, 21 May 2010 14:57:02 +0000</pubDate>
		<guid isPermaLink="false">http://nnish.wordpress.com/2009/09/17/custom-configuration-section-in-c/#comment-81</guid>
		<description><![CDATA[What can I say?  I can&#039;t tell the difference between a section and an element collection!  All fixed.]]></description>
		<content:encoded><![CDATA[<p>What can I say?  I can&#8217;t tell the difference between a section and an element collection!  All fixed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FJ</title>
		<link>http://nnish.com/2009/09/17/custom-configuration-section-in-c/#comment-80</link>
		<dc:creator><![CDATA[FJ]]></dc:creator>
		<pubDate>Fri, 21 May 2010 13:02:38 +0000</pubDate>
		<guid isPermaLink="false">http://nnish.wordpress.com/2009/09/17/custom-configuration-section-in-c/#comment-80</guid>
		<description><![CDATA[I&#039;ve followed your example (and several others!) but I cannot get the GetSection method to return anything other than NULL.  Does this method work in VS2010 do you know?]]></description>
		<content:encoded><![CDATA[<p>I&#8217;ve followed your example (and several others!) but I cannot get the GetSection method to return anything other than NULL.  Does this method work in VS2010 do you know?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nnish</title>
		<link>http://nnish.com/2009/09/17/custom-configuration-section-in-c/#comment-49</link>
		<dc:creator><![CDATA[nnish]]></dc:creator>
		<pubDate>Wed, 03 Mar 2010 10:22:29 +0000</pubDate>
		<guid isPermaLink="false">http://nnish.wordpress.com/2009/09/17/custom-configuration-section-in-c/#comment-49</guid>
		<description><![CDATA[@Moditha - Collection of JobDetails will work, If i understand it correctly that your problem is very similar to the way i use Messages Collection, If you look at each Message Element has a Name and a Value which is nothing but a Key and Value. This will work very similar to the AppSettings KeyValueConfigurationCollection. Try this out and let me know if it worked.]]></description>
		<content:encoded><![CDATA[<p>@Moditha &#8211; Collection of JobDetails will work, If i understand it correctly that your problem is very similar to the way i use Messages Collection, If you look at each Message Element has a Name and a Value which is nothing but a Key and Value. This will work very similar to the AppSettings KeyValueConfigurationCollection. Try this out and let me know if it worked.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: moditha</title>
		<link>http://nnish.com/2009/09/17/custom-configuration-section-in-c/#comment-48</link>
		<dc:creator><![CDATA[moditha]]></dc:creator>
		<pubDate>Wed, 03 Mar 2010 10:04:20 +0000</pubDate>
		<guid isPermaLink="false">http://nnish.wordpress.com/2009/09/17/custom-configuration-section-in-c/#comment-48</guid>
		<description><![CDATA[What if I want to do this?
	
		
			
				
				
				
			
			
				
				
				
				
						
		
	

The issue I am having is that my Job ConfigurationElement class has to have a property that returns the JOBDETAIL element. However, JOBDETAIL element needs to be a KeyValueConfigurationCollection since every JOBDETAIL can have different amount of key/values. I can get this to work if there is only one, JobDetail elements, but a collection of jobDetails won&#039;t work. Any ideas?]]></description>
		<content:encoded><![CDATA[<p>What if I want to do this?</p>
<p>The issue I am having is that my Job ConfigurationElement class has to have a property that returns the JOBDETAIL element. However, JOBDETAIL element needs to be a KeyValueConfigurationCollection since every JOBDETAIL can have different amount of key/values. I can get this to work if there is only one, JobDetail elements, but a collection of jobDetails won&#8217;t work. Any ideas?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mitesh</title>
		<link>http://nnish.com/2009/09/17/custom-configuration-section-in-c/#comment-47</link>
		<dc:creator><![CDATA[Mitesh]]></dc:creator>
		<pubDate>Wed, 20 Jan 2010 14:07:32 +0000</pubDate>
		<guid isPermaLink="false">http://nnish.wordpress.com/2009/09/17/custom-configuration-section-in-c/#comment-47</guid>
		<description><![CDATA[Good One... Keep on posting]]></description>
		<content:encoded><![CDATA[<p>Good One&#8230; Keep on posting</p>
]]></content:encoded>
	</item>
</channel>
</rss>

