views:

1197

answers:

1

In a desktop application needing some serious refactoring, I have several chunks of code that look like this:

private void LoadSettings()
{
    WindowState = Properties.Settings.Default.WindowState;
    Location = Properties.Settings.Default.WindowLocation;
    ...
}

private void SaveSettings()
{
    Properties.Settings.Default.WindowState = WindowState;
    Properties.Settings.Default.WindowLocation = Location;
    ...
}

What's the best way to replace this? Project-imposed constraints:

  • Visual Studio 2005
  • C# / .NET 2.0
  • Windows Forms

Update

Thanks Tundey, that looks like the way to go. For posterity, I've also found two useful tutorials: "Windows Forms User Settings in C#" and "Exploring Secrets of Persistent Application Settings".

I've asked a follow-up question about using this technique to bind a form's Size here. I separated them out to help people who search for similar issues.

+7  A: 

If you open your windows form in the designer, look in the properties box. The first item should be "(ApplicationSetting)". Under that is "(PropertyBinding)". That's where you'll find the option to do exactly what you want.

Tundey