text
stringlengths
0
13.4k
appSettings section of the configuration file.
1.
Settings can be made strongly typed. Any type which can be serialized can be used for a settings value.
2.
Application settings can be easily separated from user settings. Application settings are stored in a single
configuration file: web.config for Web sites and Web applications, and app.config, renamed as
assembly.exe.config, where assembly is the name of the executable. User settings (not used by Web projects)
are stored in a user.config file in the user's Application Data folder (which varies with the operating system
version).
3.
Application settings from class libraries can be combined into a single configuration file without risk of name
collisions, since each class library can have its own custom settings section.
In most project types, the Project Properties Designer has a Settings tab which is the starting point for creating
custom application and user settings. Initially, the Settings tab will be blank, with a single link to create a default
settings file. Clicking the link results in these changes:
1.
If a configuration file (app.config or web.config) does not exist for the project, one will be created.
2.
The Settings tab will be replaced with a grid control which enables you to create, edit, and delete individual
settings entries.
3.
In Solution Explorer, a Settings.settings item is added under the Properties special folder. Opening this
GoalKicker.com – .NET Framework Notes for Professionals
61
item will open the Settings tab.
4.
A new file with a new partial class is added under the Properties folder in the project folder. This new file is
named Settings.Designer.__ (.cs, .vb, etc.), and the class is named Settings. The class is code-generated,
so it should not be edited, but the class is a partial class, so you can extend the class by putting additional
members in a separate file. Furthermore, the class is implemented using the Singleton Pattern, exposing the
singleton instance with the property named Default.
As you add each new entry to the Settings tab, Visual Studio does these two things:
1.
Saves the setting in the configuration file, in a custom configuration section designed to be managed by the
Settings class.
2.
Creates a new member in the Settings class to read, write, and present the setting in the specific type
selected from the Settings tab.
Section 14.4: Reading strongly-typed settings from custom
section of configuration file
Starting from a new Settings class and custom configuration section:
Add an application setting named ExampleTimeout, using the time System.Timespan, and set the value to 1 minute:
Save the Project Properties, which saves the Settings tab entries, as well as re-generates the custom Settings class
and updates the project configuration file.
Use the setting from code (C#):
Program.cs
GoalKicker.com – .NET Framework Notes for Professionals
62
using System;
using System.Diagnostics;
using ConsoleApplication1.Properties;
namespace ConsoleApplication1
{