Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing application settings in C#

What is the best practice to store application settings (such as user name and password, database location ...) in C# ?

Hint: I am new to .net and C#

like image 393
Radi Avatar asked Jul 30 '10 12:07

Radi


People also ask

Where are application settings stored?

User-scope settings are stored in the user's appdata folder. Application-scope settings are stored in C:\Users\My Name\AppData\Local\My_Company\. If your settings file doesn't contain any Application-scope settings, you won't have a company folder.

How can I save application settings in a Windows Forms application?

You can use the app. config file to save application-level settings (that are the same for each user who uses your application).

Where are C# application settings saved?

User settings are saved in a file within a subfolder of the user's local hidden application data folder.

What are application settings?

Application settings enables developers to save state in their application using very little custom code, and is a replacement for dynamic properties in previous versions of the . NET Framework.


2 Answers

Application Configuration Settings that are application wide (non-user specific) belong in either app.config (for Desktop apps) or web.config (for Web apps).

Encrypting sections of a web.config file is quite simple as outlined in this Super Simple Example.

If you need to store User specific settings (like application settings, etc.) or Application wide settings not related to application configuration you can use a Settings file as described here:

User Settings in C#

like image 79
Justin Niessner Avatar answered Oct 14 '22 06:10

Justin Niessner


I'm not sure what version of .net/Visual Studio it was introduced in, but you can right click on your project, choose 'Add New Item' and select 'Settings File' from the "Add New Item" window. This provides your project with a (named by default) Settings.settings file that you can configure all the settings you want to expose in.

You can define settings that you create to be either Application or User which means you can use this single interface to control global and user settings. Once you've created a setting in the Settings.settings file using the editor that Visual Studio provides, you can access it in code like this:

// Get a Setting value
var valueOfSetting1 = Settings1.Default.Setting1;

// Modify and save a Setting value
Settings1.Default.Setting1 = "New Value";
Settings1.Default.Save();
like image 40
Rob Avatar answered Oct 14 '22 06:10

Rob