Hello Rodrigo,
From your description, you're going to programmatically read and modify the
app.config file which has persisted some hibernate connectionstring pairs,
correct?
As the configuration fragment you've provided, you are using the
"System.Configuration.NameValueSectionHandler" class to handle the
"mhibernate" connection items. I would recommend you change the handler to
"System.Configuration.AppSettingsSection" class. Because
"NameValueSectionHandler" is a very raw handler class which doesn't provide
high level encapsualted interface for accessing each items in the
collection. The "AppSettingsSection" class is a well encaspulated
one(through the new .NET 2.0 configuration model) which can help us easily
read and modify items in those cofiguration sections handled by it. To use
it, you can simply change the section handler class to "AppSettingsSection"
as below:
======================
<configuration>
<configSections>
<section name="nhibernate"
type="System.Configuration.AppSettingsSection, System.Configuration,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
restartOnExternalChanges="false"/>
.........................
=======================
then, you can use the followin like code to enumerate values and add new
values in the configuration file:
===================================
private void btnConfig_Click(object sender, EventArgs e)
{
string output = string.Empty;
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection section = config.GetSection("nhibernate")
as AppSettingsSection;
//enumerate all items in section
if (section != null)
{
foreach (string key in section.Settings.AllKeys)
{
output += "\r\n" + key + ": " +
section.Settings[key].Value;
}
}
//add new items
section.Settings.Add(new KeyValueConfigurationElement("newkey_"
+ DateTime.Now.Ticks, "newvalue"));
config.Save();
}
====================================
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.