none
How to read a section from App.config

    Question

  • I have a simple console application that must have some lists stored in the App.config file. I have been trying to read from the config file all afternoon without success, using the ConfigurationManager class.

    My config file looks like this and I want to read the items in the <Projects> section. This shouldn't be too hard, but I haven't had any success so far.

    <?xml version="1.0"?>
    <configuration>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>

      <Projects>
          <Item>Apple</Item>
          <Item>Orange</Item>
          <Item>Pineapple</Item>
          <Item>Banana</Item>
        </Projects>
     
    </configuration>

    Monday, August 27, 2012 3:35 PM

Answers

  • Hi, 1st of all you have to add (and I bet you already made it) the System.Configuration namespace, else you cannot access to ConfigurationManager class.

    ---

    Ok, then you can try with:

    - for a single value:

    string someValue = ConfigurationManager.AppSettings["ElementName"].ToString();

    - for multiple values:

    I would suggest you to use xml namespace (or Linq.Xml namespace).

    Like:

     string path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath; //path to app.Config
                //then:
                XDocument doc = XDocument.Load(path);
                var query = doc.Descendants("Projects").Select(s => new { ElementContent = s.Value }).ToList();

    Now loop through the query : foreach(var element in query) { element....}


    Mitja

    • Proposed as answer by Norkk Monday, August 27, 2012 4:45 PM
    • Marked as answer by HeLa75 Tuesday, August 28, 2012 12:48 PM
    Monday, August 27, 2012 4:07 PM
  • He still need to add configSection in app.config file in order to work.
    • Marked as answer by HeLa75 Tuesday, August 28, 2012 12:48 PM
    Monday, August 27, 2012 5:01 PM

All replies

  • HeLa75 

    In order to use ConfigurationManager class to load your custom sections, you need to:

    First, in your app.config file defines a custom section, the syntax looks like the following:

    <configSections>

        <section name="Projectstype="YourNameSpace.Projects"/>   

      </configSections>

    Second, in your c# project, create a class call Projects which inherit from ConfigurationSection class something like the following:

     

    public class Projects: ConfigurationSection
        {
            [ConfigurationProperty("Items")]
            public string Items{ get { return this["Items"] as string; } }


        }

    Third, change your current setting to

    <Projects Items="Apple,Orange,Pineapple,Banana" />

    Fourth, in your c# project when loading Projects section make sure you call:

    Projects projectItems = (Projects) ConfigurationManager.GetSection("Projects");

    string Items = projectItems.Items;

    Console.Write(Items);

    That's all. These 4 steps will help setup a very basic custom settings.

    If you are still unsure please refer to this articles:

    http://stackoverflow.com/questions/1316058/how-to-create-custom-config-section-in-app-config

    or

    http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx

    Hope this help

    Sam

    Monday, August 27, 2012 4:06 PM
  • Hi, 1st of all you have to add (and I bet you already made it) the System.Configuration namespace, else you cannot access to ConfigurationManager class.

    ---

    Ok, then you can try with:

    - for a single value:

    string someValue = ConfigurationManager.AppSettings["ElementName"].ToString();

    - for multiple values:

    I would suggest you to use xml namespace (or Linq.Xml namespace).

    Like:

     string path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath; //path to app.Config
                //then:
                XDocument doc = XDocument.Load(path);
                var query = doc.Descendants("Projects").Select(s => new { ElementContent = s.Value }).ToList();

    Now loop through the query : foreach(var element in query) { element....}


    Mitja

    • Proposed as answer by Norkk Monday, August 27, 2012 4:45 PM
    • Marked as answer by HeLa75 Tuesday, August 28, 2012 12:48 PM
    Monday, August 27, 2012 4:07 PM
  • Follow Mitja post, and it will work

    Web Developer

    Monday, August 27, 2012 4:45 PM
  • Follow Mitja post, and it will work

    Web Developer

     thx Norkk :)

    Mitja

    Monday, August 27, 2012 5:00 PM
  • He still need to add configSection in app.config file in order to work.
    • Marked as answer by HeLa75 Tuesday, August 28, 2012 12:48 PM
    Monday, August 27, 2012 5:01 PM
  • Thanks for the input!

    I used XmlDoc to load and parse the document. That seemed the easiest solution.

    Tuesday, August 28, 2012 8:28 AM
  • Thanks for the input!

    I used XmlDoc to load and parse the document. That seemed the easiest solution.


    I'm glad to see that you solve your problem, so can you please mark Mitja post as answer?

    Web Developer

    Tuesday, August 28, 2012 12:34 PM
  • One last addition. I found out that the app.config must have this structure, with the <configSections> section at the top, for things to work.

    <?xml version="1.0"?>
    <configuration>
      <configSections>
        <section name="Projects" type="YourNameSpace.Projects"/>   
      </configSections>

      <Projects>
         <Item>Apple</Item>
         <Item>Orange</Item>
         <Item>Pineapple</Item>
         <Item>Banana</Item>
       </Projects>

       <startup>
          <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
       </startup>
     
    </configuration>

    Tuesday, August 28, 2012 12:48 PM