The following maps the file and then opens it as a System.Configuration Variable:
string FilePath = System.Reflection.Assembly.GetAssembly(typeof(EncryptDecryptViewModel)).Location
FilePath += @".config";
var ConfigFileMap = new ExeConfigurationFileMap();
ConfigFileMap.ExeConfigFilename = FilePath;
Configuration LocalConfigurationManager = ConfigurationManager.OpenMappedExeConfiguration(ConfigFileMap, ConfigurationUserLevel.None);
You can now retrieve values from the assemblies configuration file much like you would through the configuration manager, however you must be a little bit more explicit in your requests.
When using System.Configuration.ConfigurationManager, the following would be valid and return a value:
string s = System.Configuration.ConfigurationManager.AppSettings["SomeSetting"];
However when using the Configuration variable LocalConfigurationManager (from the code above) a call like the normal ConfigurationManager call such as:
string s = LocalConfigurationManager.AppSettings["ConfigurationSections"];
You would get the following error when you attempt to run the code:
'System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty]' is
inaccessible due to its protection level
This is because in the ConfigurationManager the AppSettings property is a NameValueCollection. In the Configuration variable, AppSettings is actually a System.Configuration.AppSettingsSection which contains a property called Settings that is a KeyValueConfigurationCollection so to access the property the call would look like this:
string s = LocalConfigurationManager.AppSettings.Settings["SomeSetting"].Value;
For the Connection Strings Secion, the following syntax would be used
string ConnectionString = LocalConfigurationManager.ConnectionStrings.ConnectionStrings["connectionStringName"].ConnectionString;