Configuring C# - dummies

By Bill Sempf, Chuck Sphar, Stephen R. Davis

Part of C# 5.0 All-in-One For Dummies Cheat Sheet

A whole namespace of classes is designed to configure applications written in any .NET language, such as C#, VB.NET, or any one of several others. The objects within it are, to say the least, a bit tough to navigate. The configuration in .NET applications takes place in a .config file. This XML formatted file has a <configuration> node, and a whole slew of project specific nodes within. Getting a reference to the configuration requires a call to OpenExeConfiguration.

System.Configuration.Configuration config =

Once that is done, getting a reference to a section is pretty straightforward. The GetSection method just takes a string that is the node name, and because the config file is just text, you can grab it anytime.

var sectionName = “Whatever!”;
System.Configuration.AppSettingsSection section =
 (System.Configuration.AppSettingsSection) config.GetSection(sectionName);

You can add sections to the config file using the XML manipulation tools in the framework. You can use a preset group on sections for the corresponding functionality.

Section Description
Startup Describes the .NET version to use.
Runtime Lists the .NET elements to bind to the executable.
Network Describes proxy and other network settings.
Cryptography A place to keep accessible values referencing crypto resources
Configuration The most used section. It’s where you keep custom sections of the config file.
Trace/Debug Two settings that allow you to set up tracing and logging.
Application Settings (appsettings) Application scoped things like database connection strings.
Web Settings (websettings) ASP.NET specific details.