In visual studio I have solution with 3 projects in it. Project 1 must use configuration file which belongs to project 2. Normally, to access current project configuration file, code below must be used:
Private ReadOnly appSettings As Object = ConfigurationManager.AppSettings
Console.WriteLine(appSettings("myValue"))
What if we need to access configuration file which belongs to different project? Example below:
For solution many thanks to "RBT" who gave his code:
I modified it a bit to make it work in VB:
Public Function CustomConfig(value As String, Optional ByVal CustomConfigName As String = "MyCustomSettings.exe.config")
Dim configFileMap As ExeConfigurationFileMap = New ExeConfigurationFileMap()
configFileMap.ExeConfigFilename = CustomConfigName
Dim configFile = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None)
Dim section As AppSettingsSection = CType(configFile.GetSection("appSettings"), AppSettingsSection)
Dim keyValueConfigElement As String = section.Settings(value).Value
Return keyValueConfigElement
End Function
Usage:
Console.WriteLine(CustomConfig("myValueInConfigFile"))