|
Visual Studio 2005 has a great support to define application- and user-specific configuration with a "Settings" file. Creating a Settings file item, a class is created that derives from the class ApplicationSettingsBase to read application-configuration information from app.config. The values can be read using the strongly-typed class, e.g. Properties.Settings.Default.SomeSetting.
If the application configuration file does not exist, or the configuration information is not found in the file, the default values are hard-coded in the settings-class with the attribute [DefaultSettingValue]. If the values should not be hard-coded, it is possible to disable this feature by setting the property GenerateDefaultValueInCode to false.
Special attention must be taken if the configuration values should be used within a library. With a library it is possible to create a settings file item for strongly typed access to configurations. Of course, libraries don't have their own configuration files. The configuration settings used by the settings class in the library can easily be added to the configuration file of the application.
Special care must be taken if the same configuration values are shared between multiple projects. The tool-generated settings class uses configuration values that include the root namespace of the project. This behavior can be changed by applying the attribute [SettingsGroupName("MyGroup")]. Using this attribute, the group name is used with the configuration file instead of the namespace of the project:
<applicationSettings> <MyGroup> <setting name="Setting1" serializeAs="String"> <value>Some value</value> </setting> </MyGroup> </applicationSettings>
This makes it possible to share the same configuration between multiple projects although the projects can have different root namespaces.
Christian
|