Search code examples
.netapplication-settings

How to save application settings to file?


I have simple application. To be more specific this is Visual Studio Addin. Client needs to specify several settings (string, int, decimal etc)

I need to be able to load it at starup time and save if changed.

Let's say for simplicity I have all settings in one class AppSettings.

I can of course use DataContractSerializer to serialize/deserialize AppSettings but I think there must be some standard way.


Solution

  • There are Application Settings from Microsoft:

    http://msdn.microsoft.com/en-us/library/a65txexh.aspx?appId=Dev10IDEF1&l=EN-US&k=k(APPLICATIONSETTINGSOVERVIEW);k(TargetFrameworkMoniker-%22.NETFRAMEWORK&k=VERSION=V4.0%22)&rd=true

    You can use it like this:

    Properties.Settings.Default.DumpHeader = dumpHeader;
    Properties.Settings.Default.DumpFooter = dumpFooter;
    
    Properties.Settings.Default.Save();
    

    And loading works similar. But you have to define a .settings file first. It's from the WPF Framework, not sure if it works for WinForms too.

    Similar question with a similar answer:
    Best practice to save application settings in a Windows Forms Application