Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Changing Configuration Settings in a Desktop Client Application


:P
On this page:

Got a question in response to a Localization article today that asked how to store requested culture settings. In the article I recommend that easiest and most reliable way to switch cultures is to assign the culture when the application runs and allow the user to change cultures somewhere in the UI. When the culture is changed it’s up to the application to decide how to handle the actual culture change. One of the easiest things to do is write the change to a configuration setting and then exit and immediately restart the application.

.NET 2.0 gained the ability to write configuration settings in addition to easily reading them. Although this isn’t something you need frequently it’s quite handy to be able to update values easily and then write the changes back out into the configuration store. Here’s an example of a sample application that allows changing the active culture writing a new culture selection into a configuration file (in WPF):

private void lstLanguageSelections_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string origCulture = CultureInfo.CurrentUICulture.IetfLanguageTag;
    string culture = ((ComboBoxItem)this.lstLanguageSelections.SelectedValue).Tag as string;

    // static app method that acutally sets the culture
    App.SetCulture(culture, true);  // force windows to close 

    if (this.IsInitialized && culture != origCulture)
    {
        System.Configuration.Configuration config =
               ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        // Add an Application Setting.            
        config.AppSettings.Settings.Remove("Culture");
        config.AppSettings.Settings.Add("Culture", culture);

        // Save the configuration file.
        config.Save(ConfigurationSaveMode.Modified);

        // Force a reload of a changed section.
        ConfigurationManager.RefreshSection("appSettings");

        Process.Start(
            new ProcessStartInfo(Environment.CommandLine)
            {
                UseShellExecute = false,
                WindowStyle = ProcessWindowStyle.Normal
            });

        Application.Current.Shutdown();
    }
}

Notice that you have to remove the setting before adding it back in when updating the value. The code checks to see if the culture has indeed changed (a little odd since this IS a SelectionChanged event handler after all but this has to do with how WPF initially assigns values) and removes the key and adds it back in. Finally the whole file is written back out and the configuration values are refreshed so the next time you read the changed configuration key(s) they will reflect the new values.

All of this assumes the that you have appropriate permissions – you need to have read/write access in the folder where your .config file is located – most likely the applications’s startup folder, which by no means is guaranteed. Under restricted profiles (or with UAC enabled in Vista) writing into the installation folder is not allowed. So use this approach with some awareness of the security environment it runs in.

Posted in .NET  

The Voices of Reason


 

Jason Haley
June 29, 2009

# Interesting Finds: June 29, 2009

Interesting Finds: June 29, 2009

Jamin
June 29, 2009

# re: Changing Configuration Settings in a Desktop Client Application

Rick - Interesting stuff as usual. PS - thought you might want to update your "from" status, as in "from HR". Or perhaps "from Oceanside" if that's the case. Speaking of, I can see some windsurfers in the Oceanside cam right now. Looks good. And the OR marine forecast is "interesting"....

north gales 35 to 40 kt will persist through early wednesday afternoon before diminishing to 30 kt. winds will gust to 45 kt in the afternoons and evenings.

combined seas of 12 to 16 feet will continue through wednesday evening...then subside to 10 to 14 feet wednesday. the seas will be dominated by wind waves with a period of 8 seconds. seas will subside to 8 to 10 feet thursday.

Anatoly Lubarsky
July 14, 2009

# re: Changing Configuration Settings in a Desktop Client Application

This approach is wrong.

Since .NET 2.0 this can be done via Properties.Settings class in desktop app and .NET is able to handle everyhting without changing default permissions or application restart.

hope this helps

Rick Strahl
July 14, 2009

# re: Changing Configuration Settings in a Desktop Client Application

@Anatoly - Interesting, I didn't realize the Settings class actually has a Default.Save() method to write settings back to the config file.Thanks for pointing that out.

Those default app settings follow an unfortunate naming convention though for the section written out. I never actually use them and they don't work in Web apps.

The above code is more generic as you have a little more control over what gets written.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024