2

In my application I would like to persist a variable for configuration as part of the the project (I could do it with an accompanying file but that would be evil), so that a change can be made while the application is running and saved such that it can be closed and re-opened and keeping the same value.

What is the recommended or best practice approach to this in a Visual Studio 2010 C# project?

one-t
  • 333
  • 3
  • 13
  • Possible duplicate of http://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application – zeebonk Sep 24 '12 at 10:00

2 Answers2

3

Use App.config or Web.config.

Example: here

Which is the recommended best approach.

You could also save your settings in the Database (if you're using one) and load them at runtime, if you don't want to use "an accompanying file" (although having said that it is still an accompanying file). However you'd have to hard code the connection string to your DB which might be troublesome if the connection string is changed.

Also note that connection strings are usually saved in the .Config file under the tag <connectionStrings></connectionStrings>

Jonny
  • 2,787
  • 10
  • 40
  • 62
2

Make use of .Config file for saving setting. Developers can use configuration files to change settings without recompiling applications.

Configuration files contain elements, which are logical data structures that set configuration information. Within a configuration file, you use tags to mark the beginning and end of an element.

Configuration Files

and

Configuration Settings File for providing application configuration data

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263