Automatically save settings to C # output

VB.NET has the option "Automatically save settings on exit", is there an equivalent option in C # or do you need to write the following code? "

private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { Properties.Settings.Default.Save(); } 
+6
c # settings
source share
4 answers

You can use ApplicationExit instead.

 Application.ApplicationExit += new EventHandler(Application_ApplicationExit); void Application_ApplicationExit(object sender, EventArgs e) { Settings.Default.Save(); } 

Alternatively, you can also save with every change:

 Settings.Default.PropertyChanged += new PropertyChangedEventHandler(Default_PropertyChanged); void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) { Settings.Default.Save(); } 

Warning: if you are using the second approach, consider @Hans Passant's comments

+14
source share

You can do it too. If you use Windows Forms, go to the "Events" tab in the "Properties" panel of the "Design View" for the form you want to use and scroll down until you see "Close", then double-click over her.

Then add the code shown below.

 private void Form1_Closing(object sender, EventArgs e) { Properties.Settings.Default.Save(); } 
+1
source share

Yes it is. I prefer to handle save, reset, reload at the form level, because the settings are shared in the application. Calling .Reset () on FormCancel does the right thing for the user, and also calls .Save () when the form closes. If the application crashes after this point, the settings are saved. I think that saving to AppStart / Exit is not suitable for the user at the right time.

 Settings.Default.Save(); 

Other interesting methods:

 .Upgrade(); .Reset(); .Reload(); 
0
source share

according to this article he is exactly the same

-2
source share

All Articles