ConfigurationManager Seemingly Simple

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

I am trying to read the appsettings section of a configuration file
using the ConfigurationManager object (ASP.NET application) with the
following three lines but for some reason the ConfigurationManager does
not recognize there is an appSettings section in the file and the last
line throws an exception. It opens successfully but the collection is
empty. Any thoughts as to why?


Configuration configFile =

ConfigurationManager.OpenExeConfiguration(configFileAndPath);

string keyToUse = string.Empty;

keyToUse =
configFile.AppSettings.Settings["ConnectionStringToUse"].Value;


Below is my configuration file and you can certainly see that it has an
appSettings section. Any help would be appreciated.

Thanks
Mark


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Development"
connectionString="server=127.0.0.1;Application Name=Generic
Security;Database=GenericSecurity; Integrated Security = SSPI; Pooling
= True; Connection Lifetime = 15; Min Pool Size=1; Max Pool Size=1"
providerName="System.Data.SqlClient" />
<add name="Test" connectionString="data source=127.0.0.1;Integrated
Security=SSPI;Initial Catalog=GenericSecurity; MinPoolSize=1;
MaxPoolSize=1" providerName="System.Data.SqlClient" />
<add name="Production" connectionString="data
source=127.0.0.1;Integrated Security=SSPI;Initial
Catalog=GenericSecurity; MinPoolSize=1; MaxPoolSize=1"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="ConnectionStringToUse" value ="Development"/>
</appSettings>
</configuration>
 
Interestingly enough, the following seems to work. Why?

fileMap.ExeConfigFilename = configFileAndPath;
configFile =
ConfigurationManager.OpenMappedExeConfiguration(fileMap,
ConfigurationUserLevel.None);
string keyToUse = string.Empty;
keyToUse =
configFile.AppSettings.Settings["ConnectionStringToUse"].Value;
 
provided you are using the default mapping (e.g., YourAppName.exe.config )
then you can use the much shorter static , one-line:

ConfigurationManager.AppSettings[keyName];

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Mark said:
Interestingly enough, the following seems to work. Why?

fileMap.ExeConfigFilename = configFileAndPath;
configFile =
ConfigurationManager.OpenMappedExeConfiguration(fileMap,
ConfigurationUserLevel.None);
string keyToUse = string.Empty;
keyToUse =
configFile.AppSettings.Settings["ConnectionStringToUse"].Value;



I am trying to read the appsettings section of a configuration file
using the ConfigurationManager object (ASP.NET application) with the
following three lines but for some reason the ConfigurationManager does
not recognize there is an appSettings section in the file and the last
line throws an exception. It opens successfully but the collection is
empty. Any thoughts as to why?

Configuration configFile =

ConfigurationManager.OpenExeConfiguration(configFileAndPath);

string keyToUse = string.Empty;

keyToUse =
configFile.AppSettings.Settings["ConnectionStringToUse"].Value;

Below is my configuration file and you can certainly see that it has an
appSettings section. Any help would be appreciated.

Thanks
Mark

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Development"
connectionString="server=127.0.0.1;Application Name=Generic
Security;Database=GenericSecurity; Integrated Security = SSPI; Pooling
= True; Connection Lifetime = 15; Min Pool Size=1; Max Pool Size=1"
providerName="System.Data.SqlClient" />
<add name="Test" connectionString="data source=127.0.0.1;Integrated
Security=SSPI;Initial Catalog=GenericSecurity; MinPoolSize=1;
MaxPoolSize=1" providerName="System.Data.SqlClient" />
<add name="Production" connectionString="data
source=127.0.0.1;Integrated Security=SSPI;Initial
Catalog=GenericSecurity; MinPoolSize=1; MaxPoolSize=1"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="ConnectionStringToUse" value ="Development"/>
</appSettings>
</configuration>
 
Thanks but I'm not using AppName.exe.config because in my case I need
to read a configuration file for a DLL. I am using the
ConfigurationManager because the XML is formatted in the exact syntax
as you would see in a "exe.config" and works perfectly in my second
posting. My question is why is the AppSettings collection empty using
the first instantiation method:

ConfigurationManager.OpenExeConfiguration(configFileAndPath);

but works perfectly when doing this type of instantiation:

fileMap.ExeConfigFilename = configFileAndPath;
configFile = ConfigurationManager.OpenMappedExeConfiguration(fileMap,
ConfigurationUserLevel.None);


Any thoughts?

Thanks
Mark

provided you are using the default mapping (e.g., YourAppName.exe.config )
then you can use the much shorter static , one-line:

ConfigurationManager.AppSettings[keyName];

Peter

--
Co-founder, Eggheadcafe.com developer portal:http://www.eggheadcafe.com
UnBlog:http://petesbloggerama.blogspot.com



Mark said:
Interestingly enough, the following seems to work. Why?
fileMap.ExeConfigFilename = configFileAndPath;
configFile =
ConfigurationManager.OpenMappedExeConfiguration(fileMap,
ConfigurationUserLevel.None);
string keyToUse = string.Empty;
keyToUse =
configFile.AppSettings.Settings["ConnectionStringToUse"].Value;
I am trying to read the appsettings section of a configuration file
using the ConfigurationManager object (ASP.NET application) with the
following three lines but for some reason the ConfigurationManager does
not recognize there is an appSettings section in the file and the last
line throws an exception. It opens successfully but the collection is
empty. Any thoughts as to why?
Configuration configFile =
ConfigurationManager.OpenExeConfiguration(configFileAndPath);
string keyToUse = string.Empty;
keyToUse =
configFile.AppSettings.Settings["ConnectionStringToUse"].Value;
Below is my configuration file and you can certainly see that it has an
appSettings section. Any help would be appreciated.
Thanks
Mark
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Development"
connectionString="server=127.0.0.1;Application Name=Generic
Security;Database=GenericSecurity; Integrated Security = SSPI; Pooling
= True; Connection Lifetime = 15; Min Pool Size=1; Max Pool Size=1"
providerName="System.Data.SqlClient" />
<add name="Test" connectionString="data source=127.0.0.1;Integrated
Security=SSPI;Initial Catalog=GenericSecurity; MinPoolSize=1;
MaxPoolSize=1" providerName="System.Data.SqlClient" />
<add name="Production" connectionString="data
source=127.0.0.1;Integrated Security=SSPI;Initial
Catalog=GenericSecurity; MinPoolSize=1; MaxPoolSize=1"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="ConnectionStringToUse" value ="Development"/>
</appSettings>
</configuration>- Hide quoted text -- Show quoted text -
 
Back
Top