By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,949 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,949 IT Pros & Developers. It's quick & easy.

ConfigurationManager Seemingly Simple

P: n/a
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(configFi leAndPath);

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>

Nov 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Interestingly enough, the following seems to work. Why?

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

On Nov 22, 12:01 pm, "Mark" <mark_overstr...@compuserve.comwrote:
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(configFi leAndPath);

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>
Nov 22 '06 #2

P: n/a
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" wrote:
Interestingly enough, the following seems to work. Why?

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

On Nov 22, 12:01 pm, "Mark" <mark_overstr...@compuserve.comwrote:
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(configFi leAndPath);

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>

Nov 22 '06 #3

P: n/a
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(configFi leAndPath);

but works perfectly when doing this type of instantiation:

fileMap.ExeConfigFilename = configFileAndPath;
configFile = ConfigurationManager.OpenMappedExeConfiguration(fi leMap,
ConfigurationUserLevel.None);
Any thoughts?

Thanks
Mark

On Nov 22, 2:31 pm, Peter Bromberg [C# MVP]
<pbromb...@yahoo.nospammin.comwrote:
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" wrote:
Interestingly enough, the following seems to work. Why?
fileMap.ExeConfigFilename = configFileAndPath;
configFile =
ConfigurationManager.OpenMappedExeConfiguration(fi leMap,
ConfigurationUserLevel.None);
string keyToUse = string.Empty;
keyToUse =
configFile.AppSettings.Settings["ConnectionStringToUse"].Value;
On Nov 22, 12:01 pm, "Mark" <mark_overstr...@compuserve.comwrote:
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(configFi leAndPath);
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 -
Nov 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.