Solved

app.config (SIMPLE)

Posted on 2007-03-30
5
826 Views
Last Modified: 2012-06-22
I am planning on using the app.config file to read in some user defined data.  I have a number of form names that will continually change over time.

Can I have the same key listed twice?

<add key=FormName value=RothIRA>
<addkey=FormName value=TOA>

etc...

I was hoping to loop through (in code) and check for all of the FormNames.  Anything found in the appConfig.....I'll be applying logic to in my app.

Is it possible to list multiple entries with the same key name?   Is there a better way to go about this?

thx.

Knoxxx
0
Comment
Question by:JohnnyKnoxville
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
5 Comments
 
LVL 22

Expert Comment

by:_TAD_
ID: 18826425

No, each key must be unique.

On the other hand, if you use XML, and serialize the data into a class, you can easily create an array of values.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 18826472
Here's a sample from code that I use all the time:

[App.Config]



</xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <section name="Section1"
             type="ConfigSectionHandler.ConfigSectionHandler, ConfigSectionHandler" />
    <section name="Section2"
             type="ConfigSectionHandler.ConfigSectionHandler, ConfigSectionHandler" />
  </configSections>

  <Section1 type="Namespace.Class, Namespace">
     <FormName>MyForm1</FormName>
     <FormName>MyForm2</FormName>
     <FormName>MyForm3</FormName>
     <FormName>MyForm4</FormName>
     <FormName>MyForm5</FormName>
  </Section1>
</configuration>
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 18826481
[Add this class to your application]


using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;
using System.Configuration;

namespace ConfigSectionHandler
{
    /// <summary>
    /// This class contains only one method, Create().
    /// This is designed to handle custom sections in an Applications Configuration file.
    /// By Implementing <see cref="IConfigurationSectionHandler"/>, we can implement the
    /// Create method, which will provide the XmlNode from the configuration file. This is
    /// Deserialized into an object and passed back to the Caller.
    /// </summary>
    /// <example>
    /// Here is a configuration file entry in the <c>configSections</c> sectikon of the <c>App.Config</c>
    /// file.
    ///<code>      ///
    ///&lt;section name="ServerConfig" type="ConfigSectionHandler.ConfigSectionHandler, ConfigSectionHandler" /&gt;
    ///</code>
    ///This tells the CLR that there is a section further in, with a node name of <c>ServerConfig</c>. When this section
    ///is to be parsed, an object of type <c>ConfigSectionHandler.ConfigSectionHandler</c> which resides in the
    ///assembly <c>ConfigSectionHandler</c> will be instantiated. The CLR automatically calls a method in that object
    ///called <c>Create</c>
    ///</example>
    public class ConfigSectionHandler : IConfigurationSectionHandler
    {
        public ConfigSectionHandler()
            : base()
        {
        }

        #region IConfigurationSectionHandler Members

        /// <summary>
        /// A method which is called by the CLR when parsing the App.Config file. If custom sections
        /// are found, then an entry in the configuration file will tell the runtime to call this method,
        /// passing in the XmlNode required.
        /// </summary>
        /// <param name="parent">The configuration settings in a corresponding parent configuration section. Passed in via the CLR</param>
        /// <param name="configContext">An <see cref="HttpConfigurationContext"/> when Create is called from the ASP.NET configuration system. Otherwise,
        /// this parameter is reserved and is a null reference (Nothing in Visual Basic). Passed in via the CLR</param>
        /// <param name="section">The <see cref="XmlNode"/> that contains the configuration information from the configuration file.
        /// Provides direct access to the XML contents of the configuration section.       Passed in via the CLR.</param>
        /// <returns>The Deserialized object as an object</returns>
        /// <exception cref="System.Configuration.ConfigurationException">The Configuration file is not well formed,
        /// or the Custom section is not configured correctly, or the type of configuration handler was not specified correctly
        /// or the type of object was not specified correctly.
        /// or the copn</exception>
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            try
            {
                XPathNavigator xNav = section.CreateNavigator();
                string typeOfObject = (string)xNav.Evaluate("string(@type)");
                Type t = Type.GetType(typeOfObject);
                XmlSerializer ser = new XmlSerializer(t);
                XmlNodeReader xNodeReader = new XmlNodeReader(section);
                return ser.Deserialize(xNodeReader);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException.Message.Length > 0)
                    Console.WriteLine(e.InnerException.Message);
                return null;
            }
           
        }
        #endregion
    }
}
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 18826504
[Create a class that you serialize your app.config data to]

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Application1
{
    [XmlRoot("Section1")]
    public class FirstSection
    {
        [XmlElement("FormName")]
        public string[] _formName;    }
}
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 50 total points
ID: 18826530
[Putting it all together]

<Button1_Click>

// use configuration manager class to pull proper app.config section
// and serialize it to a class
Section1 s1 = (Section1)ConfigurationManager.GetSection("Section1");

// itterate through class data collection
for(int i=0; i<s1._formName.Length; i++)
   Console.Writeline(s1._formName[i]);
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question