Simple User Settings in a C# Windows Forms Application

There is a lot of confusion and long programming blog posts on application settings for a Windows Forms Application in C#.  I needed a quick and simple way to save a few user specific settings such as com port settings and names of labels on a customizable GUI.

I have used the Python config package written by Vinay Sajip in a previous project.  The nice thing about the package was that it opened a config file and made an object which could be accessed as config.ports.baudrate.  Since Python is not typed the config file, which is simply Python code without any punctuation marks, is flexible, fast and easy to create.

I had hoped that C# would have an easy way to build something similar.  I found an post early on suggesting using app.config.  After reading MANY posts on configuration and trying a lot of code that would not work on Visual Studio 2011 I had about given up and started writing my own.

Luckily I found a simple solution. A settings file is already included in a Windows Forms Application.
  • In the Solutions Explorer window expand Properties.
  • Double click the Settings.settings file and  an editor will open.
  • Type the name of the setting, choose a type for the data, choose "Users Setting" in scope if you want the setting to be changed at run time, and enter a default value.
  • To access the variable in code use Properties.Settings.Default.yoursettingname.
  • To save new settings at run time use Properties.Settings.Default.Save which will create a new file.  Note that when you change a setting the default value will not change in setting file editor.

Comments

Popular posts from this blog

DIY CNC Milling Machine Control Panel for EMC2

Power Supply for CNC

Maintenance of a 30 Year Old CNC Milling Machine