Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8209457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:43:37+00:00 2026-06-07T09:43:37+00:00

I created a configuration file App.config containing all the operating parameters relating to my

  • 0

I created a configuration file App.config containing all the operating parameters relating to my application.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="MinConnectionCount" value="2"/>
    <add key="MaxConnectionCount" value="7"/>
  </appSettings>
</configuration>

In order to read these values ​​and make them available to the entire application, just use the AppSettings property of the ConfigurationManager class as follows:

try
{
    NameValueCollection myAppSettings = ConfigurationManager.AppSettings;

    byte min = byte.Parse(myAppSettings["MinConnectionCount"]);
    byte max = byte.Parse(myAppSettings["MaxConnectionCount"]);
}
catch (ConfigurationErrorsException e)
{
    throw e;
}

The values ​​read by the AppSettings property are of string type, so you must perform the necessary conversions and checks:

  • You must make the appropriate conversions, ensuring that there is no parsing errors. It could be useful to verify that the values ​​obtained by the conversion are consistent with the application domain: for example, the minimum number of connections must be less than the maximum number.
  • Anything that involves loading the configuration must be done at application startup, because the loaded parameters could be used to allocate any data structures such as tables, queues, etc.. If they were to be encountered errors while loading the configuration, the application should use a default configuration: errors may be reported by an alert.
  • Finally, the configuration must be read-only, meaning they should not be changed while the application runs. This also simplifies access by multiple threads.

In order to achieve these targets, I created a class that loads the configuration from the file and makes available, via the appropriate property, the converted parameters.

public class MyConfiguration
{
    enum Errors
    {
        CONNECTION_RANGES_ERROR
        // some other errors
    }

    private byte m_MinConnectionCount;
    private byte m_MaxConnectionCount;

    private readonly List<Errors> m_ConfigErrors = new List<Errors>();

    public MyConfiguration()
    {
        try
        {
            NameValueCollection appSettings = ConfigurationManager.AppSettings;

            if (!SetConnectionRanges(appSettings["MinConnectionCount"], appSettings["MaxConnectionCount"]))
                m_ConfigErrors.Add(Errors.CONNECTION_RANGES_ERROR);

            // ...
        }
        catch (ConfigurationErrorsException e)
        {
            throw e;
        }
    }

    private bool SetConnectionRanges(string minCountStr, string maxCountStr)
    {
        if (byte.TryParse(minCountStr, out m_MinConnectionCount)
            && m_MinConnectionCount > 0
            && byte.TryParse(maxCountStr, out m_MaxConnectionCount)
            && m_MaxConnectionCount > m_MinConnectionCount)
            return true;

        m_MinConnectionCount = 2;
        m_MaxConnectionCount = 7;
        return false;
    }

    public byte MinConnectionCount { get { return m_MinConnectionCount; } }

    public byte MaxConnectionCount { get { return m_MaxConnectionCount; } }

    public List<Errors> GetErrors() { return m_ConfigErrors; }
}

The GetErrors() method allows to get a list of errors that occurred during the loading of configuration parameters: the user may be notified of these errors, and may be informed that the application has used the default values ​​for parameters.

  1. How to make sure that this class is first instantiated during application startup?
  2. I could apply the singleton pattern to this class: in this way I should just invoke an Instance property as the first statement of the Main method. But where should I invoke the Instance property in a WPF application?
  3. Rather than using the singleton pattern, I could create a global variable for MyConfiguration class and invoke the GetErrors() method as first statement of the Main method. But where should I invoke the GetErrors() method in a WPF application?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:43:39+00:00Added an answer on June 7, 2026 at 9:43 am

    Change your class to be static to ensure a single instance of it.

    Then you can invoke MyConfiguration.GetErrors() in the Load event of the first form you show and report errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created an App.config file in my WPF application: <?xml version="1.0" encoding="utf-8" ?> <configuration>
I've stored configuration of my application in the app.config, by Visual Studio I've created
Is it possible to create a custom configuration file (other than app.config) that can
When I develop a WCF service/client with a configuration file, a new file app.config
in common, hibernate sessionfactory is created in spring configuration file (eg spring-dao.xml) like; <bean
I added a custom section to my app.config file for a Windows Forms Application.
I want to create my Application directory to save my configuration file. but blackberry
I have created two DbContexts, one is for application configuration, the second is for
I created an app.config transform for my WinForms project using Dan Abramov's solution here
I've created a custom binding and want to make it configurable over App.config. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.