74

I am actually in the learning phase of .NET related stuff and I was exploring how to save the application. I ended up writing my own class which saves the settings in an XML file and then I found that .NET itself supports saving application settings.

But I found 2 ways to do that. When I open add new item dialog in Visual Studio 2008, it gives option to create Settings file (.settings) or a configuration file (.config). Whats the difference between both and in what scenario they should be used?

3 Answers 3

55

UPDATE: In ASP.NET Core Land, configuration is no longer managed via either of these - see this fantastic writeup from Travis Illig with the a-z on Microsoft.Extension.Configuration and Microsoft.Extensions.Configuration.Binder which are effectively a superset of all this


Settings (Both from a .settings set and Configuration.AppSettings), are stored in the .config file [alongside lots of other stuff].

The difference is that the .settings stuff [which was added in .NET 2.0 / VS2005] layers a strongly typed class on top of a set of settings that belong together whereas Configuration.AppSettings just lets you retrieve strings, forcing you to do any conversions, and doesnt have the notion of defaults. (the Configuration class has actually been shunted into a side assembly to reflect this - you need to add a reference to System.Configuration explicitly if you want it).

Adding a .settings to your project will result in an app.config being added to house the settings if you dont already have one. The class which reads the settings is automatically generated each time you change the list of settings for your component/application.

Other features of .Settings is the ability to designate some settings as user-specific (and also to save the user-specific settings with a single call).

The best reason of all to use .Settings is generally that you gain the ability to clearly identify who is using which setting in a code base by following usages of properties (and each set is a separate block in the XML file). Configuration.appSettings is more global in it's nature - it's just a bag of properties and you dont know which DLL, subsystem or class depends on a particular setting entry. See this blog post from Steven Smith for much more.

Finally, if you still haven't read enough about settings management, you're not going to beat this Rick Strahl post on the subject for completeness or sheer quantities of ideas and angles.

ASIDE: There's also the ASP.NET vNext Configuration stuff, outlined in this article which is quite flexible and offers a different angle on configuration settings management.

6
  • 1
    Thanks for the answer. Just confused about one point: There are 2 type of settings. One is app setting and other is user setting. If both types of settings are stored in config file, how will user settings be different for different users?
    – Hemant
    May 26, 2009 at 10:43
  • 3
    Sorry for omitting mention of that. User settings is one of the reason why defaults exist (if the user hasnt used the app yet, the defaults apply until a change is saved). The user settings are stored in a separate configuration file in the user's profile (Users\XXX) directory. User settings can be updated too (System ones cannot be using the default API and should not - they live in Program Files and it's not possible to securely control multiple user access to there.) May 26, 2009 at 11:06
  • 1
    where does configuration manager fit into all this? Im trying and failing to get that to tell me where my settings.settings lives and then save a blank config there
    – JonnyRaa
    Jan 10, 2014 at 14:38
  • @JonnyLeeds Not sure what you mean so I'll try to expand to see if it helps you.... Your .settings file just defines the schema and gets compiled into a codebehind. The actual values live in the .config file which will be in a user profile dir (non-user ones are not writeable). The data lives in the same place as ConfigurationManager.AppSettings puts it, it's just in a custom format with stronger typing. You can do things like ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.PerUserRoamingAndLocal ).FilePath but before I tell you that.... why? Jan 13, 2014 at 11:18
  • @RubenBartelink thanks for the response. That's pretty much the conclusion I came to - just posted a Q&A to document my pain here!
    – JonnyRaa
    Jan 13, 2014 at 11:22
5

A settings file is a resource file in which you specify the different settings and their default value.

The values themselves are configured in the application configuration file (.config file).

A settings file is never deployed, so you will need the config file to do the configuration.

2
  • While I'm in picky mode (just updated my own post - lots missing in there) While a .settings file is an XML file, I'm reasonably sure it's not bolund in as a Resource, is it? Also, user-specific values get stored in a separate area, outside of the .config file. The .config file needs to be deployed if you've got non-default settings (attributes within the generated settings class manage the defaulting of the values if nothing is specified in the config file.) Nov 10, 2009 at 8:42
  • While the Settings.settings xml file isn't deployed, the Settings.Designer.cs file gets compiled into the application, doesn't it? Nov 17, 2017 at 19:45
-1

The app.config file is stored in the same directory as the app. Normal users will not have write permissions (e.g. in "Program Files").

The settings file should be stored in the users "AppData" directory (where he has r/w permissions).

So use the settings file for user configurable options.

1
  • Not quite right, according to other answers: stackoverflow.com/a/909712/199364 , stackoverflow.com/a/13072966/199364 .settings (and Visual Studio's Settings support) can be used both for one-time application config and per-user config. .settings doesn't exist at run-time; what exists at run-time is app.config in app folder, and [user] .config in user's data folder. Aug 13, 2017 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.