I want to read the key-value pair from AppSettings inside the configuration file but I couldn’t see my custom AppSettings. I guess the program doesn’t see my custom configuration file. Any recommendations on that? thanks! I don’t want to place my key-value pair in the settings file because of security issues.
I added System.Configuration as a reference and tried the code below as this answer suggests but it didn't work.
Working with Rhino 6.30.20288.16410 and .NET Framework 4.6.2
Config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="dbConnStr" value="MyConnectionString"/>
</appSettings>
</configuration>
Code:
private static string GetAppSetting(Configuration config, string key)
{
KeyValueConfigurationElement element = config.AppSettings.Settings[key];
if (element != null)
{
string value = element.Value;
if (!string.IsNullOrEmpty(value))
return value;
}
return string.Empty;
}
private static string GetConnStr(string key)
{
Configuration config = null;
string exeConfigPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
try
{
config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
}
catch (Exception ex)
{
//handle errror here.. means DLL has no sattelite configuration file.
}
if (config != null)
{
string myValue = GetAppSetting(config, key);
return myValue;
}
return "";
}