ConfigurationManager Seemingly Simple

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>
 
M

Mark

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;
 
G

Guest

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>
 
M

Mark

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 -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top