Using Settings

With these typed properties in place and the Default static property to expose an instance of our generated Settings class, usage is no different from any other CLR object, as you can see in Example 2-7.

Example 2-7. Using the Settings Designer-generated class public partial class Windowl : Window { string[] excuses = {...};

public Windowl() { InitializeComponent();

this.newExcuseButton.Click += newExcuseButton_Click;

// If there is no "last excuse," show a random excuse if( string.IsNullOrEmpty(Properties.Settings.Default.LastExcuse) ) {


// Show the excuse from the last session else {

excuseTextBlock.Text = Properties.Settings.Default.LastExcuse;

void newExcuseButton_Click(object sender, RoutedEventArgs e) { ShowNextExcuse();

Example 2-7. Using the Settings Designer-generated class (continued)

Random rnd = new Random(); void ShowNextExcuse() { // Pick a random excuse, saving it for the next session // and checking for animals do {

Properties.Settings.Default.LastExcuse = excuses[rnd.Next(excuses.Length - 1)];

while( Properties.Settings.Default.ExcludeAnimalExcuses && HasAnimal(Properties.Settings.Default.LastExcuse) );

// Show the current excuse excuseTextBlock.Text = Properties.Settings.Default.LastExcuse;

bool HasAnimal(string excuse) {...}

protected override void OnClosed(EventArgs e) { base.OnClosed(e);

// Save user settings between sessions Properties.Settings.Default.Save();

In Example 2-7, we're using the LastExcuse user setting to restore the last excuse the user saw when running the application previously, changing it each time a new excuse is generated. The ExcludeAnimalExcuses application setting is checked to exclude animal-based excuses, but it is never set.* To store user settings that change during an application's session, we're calling the Save method on the Settings object from the ApplicationSettingsBase base class. This class does the magic of not only keeping the settings in memory and notifying you when a setting changes (if you choose to care), but also automatically loading the settings when the application is loaded, saving on demand.

To help with the loading and saving, the ApplicationSettingsBase uses a settings provider, which is a pluggable class that knows how to read/write application settings (e.g., from the local filesystem, from the Registry, from a network server, etc.). The only settings provider that comes out of the box in .NET 2.0 is the one that writes to disk in a way that's safe to use from even partial trust applications (like an XBAP), but it's not hard to plug in your own settings provider if you need other behavior.t

* There is no configuration API to set an application setting.

t The SDK comes with custom settings provider samples that use a web service and the Registry. I didn't like the one based on the Registry, so I updated it and wrote a little article about the experience of writing and using a custom settings provider. It's available at aspx?content=dotnet2customsettingsprovider.htm (

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment