mildse7en
mildse7en

Reputation: 532

How can I save form settings?

I was just wondering if anyone has any input on how to save a C# Winform setting?

Currently, I have a form that has various radio buttons, directory browsers, date pickers etc. I was wondering what is the best strategy to save these settings to an external file that can be loaded at a later date. So essentially each configuration can be loaded, executed, and then another configuration loaded. Also, the configuration can be passed across installations / users.

Upvotes: 2

Views: 395

Answers (2)

SyntaxError
SyntaxError

Reputation: 1752

Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method. These settings are saved in the User.config file.

Write and Persist User Settings at Run Time

Access the user setting and assign it a new value, as shown in the following example:

Properties.Settings.Default.myColor = Color.UserGreen;

If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:

Properties.Settings.Default.Save();

Upvotes: 1

bash0r
bash0r

Reputation: 635

I solved this problem with a class or struct which contains all settings. My form-class had a constructor which accepted such a setting-instance.

This settings-class/-struct was implementing ISerializable. So you can save it easily into files and load it from.

This is by far not the best way to do it, but it is quiet easy to implement.

Upvotes: 0

Related Questions