The concept of building configurable applications and utilizing application configuration files has been around for years. In general the idea behind the use of application configuration files stems from the ability to dictate program behavior without the need to change code and recompile.
The appSettings section of the configuration file is inherently designed to make it extremely easy to retrieve values by simply passing the name of the attribute in question. The anatomy of the appSettings section consists of one or many "add" elements with corresponding "key" and "value" attributes. When adding an App.config note that this section is not auto-generated and must be added manually. The following App.config will be used for the example:
| <?xml version="1.0" encoding="utf-8" ?> |
| <configuration> |
| <appSettings> |
| <add key="1" value="foo"/> |
| <add key="2" value="bar"/> |
| <add key="3" value="baz"/> |
| </appSettings> |
| </configuration> |
The following example illustrates the retrieval of values from appSettings section of the above configuration file based on a given key name.
| using System; |
| using System.Configuration; |
| |
| namespace ReadFromAppConfig |
| { |
| class Program |
| { |
| static void Main(string[] args) |
| { |
| string str1 = ConfigurationSettings.AppSettings["1"]; |
| string str2 = ConfigurationSettings.AppSettings["2"]; |
| string str3 = ConfigurationSettings.AppSettings["3"]; |
| Console.WriteLine(str1 + " " + str2 + " " + str3); |
| } |
| } |
| } |