Hello Mark,
Thank you so much for your great feedbacks on the MSDN articles about
AppSettingsSection.Settings property. Our database has logged the error in
the first article
http://msdn2.microsoft.com/en-us/library/system.configuration.appsettingssec
tion.settings(VS.80).aspx. Instead of using NameValueCollection, it should
have been talking about KeyValueConfigurationCollection. A newer version is
in
http://msdn2.microsoft.com/en-us/library/system.configuration.appsettingssec
tion.settings.aspx. However, as you said, it still seems using an improper
example. I have filed an request to the MSDN team to confirm the issue.
Before I receive their response, please kindly refer to the following
example about AppSettingsSection.Settings property. Sorry for the
inconveniences.
For web application:
Configuration configuration =
WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection appSettingsSection =
(AppSettingsSection)configuration.GetSection("appSettings");
// or we can call configuration.AppSettings.Setting
if (appSettingsSection != null) {
foreach (string key in appSettingsSection.Settings.AllKeys) {
Response.Write(key);
}
}
For exe application:
string exePath = System.IO.Path.Combine(Environment.CurrentDirectory,
"ConsoleApplication1.exe");
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
AppSettingsSection appSettingsSection =
(AppSettingsSection)config.GetSection("appSettings");
// or we can call configuration.AppSettings.Setting
foreach (string key in appSettingsSection.Settings.AllKeys)
{
Console.Write(key);
}
For your question about the difference between NameValueCollection and
KeyValueConfigurationCollection: both implement the interface Icollection
and Ienumerable, but NameValueCollection also implements ISerializable and
IdeserializationCallback. Besides, KeyValueConfigurationCollection does not
support a "Set" method. We could use NameValueCollection.Set to reset a
key's value.
In the configuration file, if a key has multiple values, the default
behavior of ConfigurationManager.AppSettings and
Configuration.AppSettings.Settings is to overwrite the previous value with
the new. That is to say, if in <appSettings> tag, there are items: <add
key="TestKey" value="1"/> <add key="TestKey" value="2"/>,
Configuration.AppSettings.Settings or ConfigurationManager.AppSettings only
returns "2" as "TestKey" by design. Internally, it is using
collection[key] = value; instead of collection.Add(key, value);, therefore,
the old value will be overwritten by the new one, even if
NameValueCollection has methods like GetValues. The development team has
confirmed that it is by design because in majority of the cases, a user
wants to overwrite the value (such as in lower level web.config file for
asp.net applications) instead of adding one more value under the same key.
I am sorry if this design decision does not fit your situation. In order to
support multiple values for a same key, we need to customize the
ConfigurationSectionHandler. Please refer to the following article for more
detailed information.
http://www.codeproject.com/dotnet/namevaluemultiple.asp
Sincerely,
Jialiang Ge (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document:
http://blogs.msdn.com/msdnts/pages/postingAlias.aspx
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.