While somewhat of a tangent from your question but you may find it helpful nonetheless.
I would recommend looking at a custom ConfigurationSection which allows you to define more complex configuration hierarchies that are strongly-typed. I find it much easier to use a custom configuration section than having to remember a bunch of magic strings in the appSettings element and also allows you to specific which values are required, what the defaults values are, etc.
Using a custom configuration section you could create a configuration type like:
public class UboldiConfigurationSection : System.Configuration.ConfigurationSection {
[ConfigurationProperty("schoolName")]
public string SchoolName {
get { return (string)this["schoolName"]; }
set { this["schoolName"] = value; }
}
}
Then to load that configuration type:
public static class UboldiApplcationUboldiApplication {
public static UboldiConfigurationSection Config { get; internal set; }
public static void Initialize() {
Config = ConfigurationManager.GetSection("uboldi") as UboldiConfigurationSection;
}
}
The app.config then would look something like this:
<configuration>
<configSections>
<section name="uboldi" type="Uboldi.UboldiConfigurationSection, Uboldi" />
</configSections>
<uboldi schoolName="Fillmore Central" />
</configuration>
Lastly, you use the configuration by:
public void Test() {
//This only needs to be done once, presumably in your Program.Main method
UboldiApplication.Initialize();
var name = UboldiApplication.Config.SchoolName;
}
A couple of notes:
- You'll need to reference the System.Configuration assembly as it's not usually referenced in VS by default.
- The
ConfigurationManager.GetSection("uboldi")is expecting the name of the section in the app.config file. You'll note that this matches in the example above. - The section element in the app.config file uses the standard .Net type name convention to locate the specified configuration section. In this example I am assuming that the
UboldiConfigurationSectiontype is the Uboldi namespace and in an Uboldi assembly (dll or exe). - You can add hierarchy by creating
ConfigurationElementsub classes and using them as properties in your configuration section and elements. - The link above is for a Web.config, but the same thing is possible in an app.config file.