Public Application Settings
Application settings, created using Visual Studio's project properties, are of internal scope by default, allowing them to be used from within the same assembly. The scope can be changed to public to allow settings to be shared between projects.
Internal and Public Settings
In a previous article I explained how application settings can be created in Visual Studio and how these settings can be read and updated in code or using an XML configuration file. When created using the methods described, the settings are given an internal scope, meaning that they are available to any class in the same assembly. Sometimes you will wish to create a central repository of application settings that can be reused by several projects, or that can be read from an automated test suite. In these cases you can change the scope of the settings to public.
Public settings are available to any project that includes a reference to the assembly that contains them. Often the settings will be held in a dynamic linked library (DLL). A reference to the DLL can then be added for each other project that wishes to access the information.
Specifying Public Settings
Changing the scope of the settings in a project is simple when using a designer in Visual Studio. First you must view the settings page. This is achieved by right-clicking the project within the Solution Explorer and choosing "Properties" from the context-sensitive menu that appears. When the project properties are displayed, select the Settings section to display the current application settings. You should see a drop-down list on the toolbar at the top of the screen, labelled "Access Modifier". By default this is set to "Internal". To make the settings public, change the selection to "Public".
Public Settings and Configuration Files
When you add application settings to a class library project, a configuration file for the DLL is created. For example, if the DLL is named, "ProjectSettings.dll", the configuration file will be "ProjectSettings.dll.config". This configuration file contains the XML that defines the settings. However, the file will not be read if you deploy it with the final product. Instead, the default settings will be used. If the file is never modified you may not notice this problem. If the XML is altered, the new settings will be ignored.
To enable the settings to be changed within a configuration file you must copy the information from the DLL's configuration file to the configuration file of the main application. There are two elements that must be added. Firstly, the entire contents of the "configSections" XML section should be copied to the same element of the executable's configuration file. This defines a new configuration section containing the settings used by the DLL. Next, copy the contents of the "userSettings" section into the executable's XML file. You can now edit the settings in the main configuration file to affect the values provided by the DLL. The DLL's configuration file need not be shipped with the software.
25 August 2010