.Net Configuration (App.Config/Web.Config/Settings.Settings)

Reading settings from app.config or web.config in .NET

You'll need to add a reference to System.Configuration in your project's references folder.

You should definitely be using the ConfigurationManager over the obsolete ConfigurationSettings.

How to use .NET configuration files (app.config, settings.settings) to save and restore all application data?

Since I did not receive any other answers and was not happy with my previous solution, I asked the question again, did some more research, and was able to come up with a better answer. See How to load a separate Application Settings file dynamically and merge with current settings? for the code and explanation.

Settings.settings vs. app.config in .NET desktop app

From a .NET Framework point of view (not speaking of tools - Visual Studio - for the moment), historically, there was only [app.exe].config (in fact, it's what the AppDomain defines as the configuration file. The name is defined by the AppDomain, that's why it's web.config for web apps...) and machine.config. 'app' is deployed together with the application, 'machine' is for the whole machine. They were supposed to be 'quite' read-only for the average user. It's possible to change them, but it was not the idea.

But how can I save end user preferences then? That's why [user].config was introduced (I believe with .NET 2). The official documentation says this:

The configuration system that was originally released with the .NET
Framework supports providing static application configuration data
through either the local computer's machine.config file or within an
app.exe.config file that you deploy with your application. The
LocalFileSettingsProvider class expands this native support in the
following ways:

1) Application-scoped settings can be stored in either the machine.config
or app.exe.config files. Machine.config is always read-only, while
app.exe.config is restricted by security considerations to read-only
for most applications.

2) User-scoped settings can be stored in app.exe.config files, in which
case they are treated as static defaults.

3) Non-default user-scoped settings are stored in a new file,
user.config, where user is the user name of the person currently
executing the application. You can specify a default for a user-scoped
setting with DefaultSettingValueAttribute. Because user-scoped
settings often change during application execution, user.config is
always read/write.

So from a .NET Framework point of view, there is only one 3-layer mechanism.

Now, Visual Studio just tries to help you by generating the type-safe code for the final read/write settings. Most of the time, that [user].config file does not exists and a setting value will be defined by what's in the DefaultSettingValueAttribute (defined for each setting), or use what's been defined statically in the app.config. That's why Visual Studio also updates the app.config file so you can define static defaults to the settings. But you can perfectly delete all that app.config stuff.

App.config v.s. Web.config: which has priority?

From MSDN for .NET 4.5:

When configuring a service in Visual Studio, use either a Web.config file or an App.config file to specify the settings. The choice of the configuration file name is determined by the hosting environment you choose for the service. If you are using IIS to host your service, use a Web.config file. If you are using any other hosting environment, use an App.config file.

In Visual Studio, the file named App.config is used to create the final configuration file. The final name actually used for the configuration depends on the assembly name. For example, an assembly named "Cohowinery.exe" has a final configuration file name of "Cohowinery.exe.config". However, you only need to modify the App.config file. Changes made to that file are automatically made to the final application configuration file at compile time.

In using an App.config, file the configuration system merges the App.config file with content of the Machine.config file when the application starts and the configuration is applied. This mechanism allows machine-wide settings to be defined in the Machine.config file. The App.config file can be used to override the settings of the Machine.config file; you can also lock in the settings in Machine.config file so that they get used. In the Web.config case, the configuration system merges the Web.config files in all directories leading up to the application directory into the configuration that gets applied. For more information about configuration and the setting priorities, see topics in the System.Configuration namespace.

Here's also a great post for those using MS Azure, explaining the differences b/w ApplicationSettings, appSettings (app.config/web.config) and ConfigurationSettings (.csdef / .cscfg):

http://haishibai.blogspot.com/2012/09/windows-azure-cloud-service.html

Settings.settings vs app.config?

Settings.settings is the designer file for Visual Studio to generate the settings class like how .resx files hold resources.

The settings have to be stored somewhere, but they aren't stored in Settings.settings (the default ones are). They are stored in .config files.

With the generated class, you can update the app.config and not worry about manipulating the XML.

web.config and app.config confusion

Create a new sectionGroup in configSections called applicationSettings and paste your app.config configuration into web.config as shown below and then you can override your app.config settings.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Playground.ConfigurationOverride.DataAccess.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Playground.ConfigurationOverride.DataAccess.Properties.Settings>
<setting name="MySetting" serializeAs="String">
<value>Setting in DataAccess</value>
</setting>
</Playground.ConfigurationOverride.DataAccess.Properties.Settings>
</applicationSettings>
</configuration>

Getting configuration settings from web.config/app.config using class library

If you're not after structured settings, the appSettings section just takes key-value pairs:

<appSettings>
<add key="ADIImageRoot" value="C:\DataTemp\ADI\Original\" />
<add key="ADIImageVariantsRoot" value="C:\DataTemp\ADI\Variants\" />
</appSettings>

This will enable you to access them via the AppSettings dictionary:

ConfigurationManager.AppSettings["ADIImageVariantsRoot"]

As you would expect.

Alternatively, if you need more structure to your configuration (i.e. more than just strings, or a collection of settings), you can look into using a configuration section of your own, using a ConfigurationSection, and its relevant parts.



Related Topics



Leave a reply



Submit