3

What is the preferred way of reading an application's configuration file in .NET 4? I've seen several articles on how to do with .NET 2. I don't know if things have changed/improved with .NET 4.

2 Answers 2

10

The ConfigurationManager is still the preferred way of reading the application config and web config files.

To use it you will first need to add a reference in your project to System.Configuration.

Then you will need to add a reference to it in your class with:

using System.Configuration;

Once you have done this you will be able to access things like your AppSettings and ConnectionStrings by calling these static properties on the ConfigurationManager class.

e.g.

ConfigurationManager.AppSettings["settingname"];
Sign up to request clarification or add additional context in comments.

Comments

2

Most developers seem to be happy with the string-based ConfigurationManager.AppSettings style of configuration, but there is another way: strongly typed configuration.

The MSDN reference is here: http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx

In a nutshell, you can define your own configuration setting section and have your own strongly typed configuration items in there. Amongst other things this

  • (as the name implies) can force use of enumerated options
  • allows you to define user-based or application-based settings
  • allows you to define defaults

The main downside is that it's a bit of a faff to get going as there's quite a bit of code to implement and test.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.