Netstep
Netstep

Reputation: 522

How to store app settings?

I am doing a wpf application and I use application settings in app.config file:

var datapath = Properties.Settings.Default.DataSource;

...

How to make application to load the app.config file, if it present from the same location where exe file run from., so user can change app.config and run it with new settings. By default app.config ignored and application uses always default settings

Upvotes: 2

Views: 1711

Answers (1)

kyrylomyr
kyrylomyr

Reputation: 12632

You can save and restore any settings using Binding in TwoWay mode. TwoWay needed for auto storing changes of property. For example binding height of the window:

Height="{Binding Source={x:Static self:Properties.Settings.Default}, 
                         Path=ApplicationHeight, Mode=TwoWay}"

To make binding works you need to create setting record in project properties (with name ApplicationHeight in example). To save setting on app closing use:

Properties.Settings.Default.Save();

in Window.Closed or Application.Exit events.

Upvotes: 5

Related Questions