Hosea146
Hosea146

Reputation: 7702

Reading Application Configuration File - .NET 4

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.

Upvotes: 3

Views: 4737

Answers (2)

Jeremy McGee
Jeremy McGee

Reputation: 25200

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.

Upvotes: 2

startupsmith
startupsmith

Reputation: 5764

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"];

Upvotes: 10

Related Questions