Access Configuration Settings from other Modules
The configuration settings are typically accessed using the Properties class/namespace as follows:
string myFolder = Properties.Settings.Default.FolderToUse;
Where the 'FolderToUse' is the property setting specified in the Settings file.
However, these settings can only be accessed in the entry assembly.
These settings cannot be accessed from other modules (BL or DL modules).
Use the following technique to allow access to these settings in other modules by passing the settings object to classes in other modules.
In the main method, in the entry module, access one of the existing properties the normal way. If this is not done, the settings do not get loaded.
string someProperty = Properties.Settings.Default.RandomSetting;
It does not seem to matter which property is retrieved.
Pass the settings object using the following. It can be a constructor or a simple method.
public class CustomBL { // declare a member variable to hold the settings object // mark it 'readonly' so that it can only be set in the constructor and then never be changed. private readonly global::System.Configuration.ApplicationSettingsBase m_ourSettings; public CustomBL(global::System.Configuration.ApplicationSettingsBase p_Settings) { // save the reference to the settings object to our member variable this.m_ourSettings = p_Settings; } }
The constructor would be called as follows:
var bl = new CustomBL(Properties.Settings.Default);
Inside the methods of the module class, the properties would be accessed as follows.
string property1 = this.m_ourSettings.PropertyValues["FirstProperty"].PropertyValue.ToString(); string property2 = this.m_ourSettings.PropertyValues["SecondProperty"].PropertyValue.ToString();
The collection item property 'PropertyValue' is of type 'object' and must be cast to the desired data type. Be aware that the value here may be null.