BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Configuration
.NET 2.0+

Using Custom Classes with Application Settings

Application settings can be created using a project's property windows, with those values being transferred into configuration files and classes that simplify their use. The settings data can be of many different types, including custom classes.

Using the Settings

With the settings now created we can use them like we would any others. To demonstrate, add the following using directive to provide simplified access to the settings:

using SettingsTest.Properties;

Now add the following code to the Main method and run the program. This code reads the two settings and outputs their values to the console. Try compiling the program and running it outside of Visual Studio, modifying the configuration file to see the output change accordingly.

Employee emp = Settings.Default.DefaultEmployee;
Console.WriteLine("Employee {0} - {1}", emp.Name, emp.Position);

Room room = Settings.Default.DefaultRoom;
Console.WriteLine("Room {0} - {1}", room.RoomNumber, room.Location);

/* OUTPUT

Employee John - Developer
Room 1 - Reception

*/
21 August 2011