Hi Marc,
In .NET 1.x, we could set the 'file' attribute of the appSettings element
of a config file to specify an external file that contains custom
application configuration settings.
In .NET 2.0, we can now include configuration settings in a separate file
for all configuration elements that support the configSource attribute.
However, when we use the configSource attribute, we must move the entire
section to the separate file because there is no merging of element
settings.
There is a one-time write to the exe.config file when using the
configSource attribute.
I will illuminate it with a sample.
1.Create a WinForm application.
2. Double-click the Settings.settings under the Properties folder in the
Solution Explorer.
3. Add a setting named 'Setting1' with the value of 'setting1' in the
Settings.setting. Save the changes.
4. Double-click the app.config file in the Solution Explorer and add a
section named "MyExternalSection" as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="TestProject.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<!--Add a custom section here-->
<section name="MyExternalSection"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</sectionGroup>
</configSections>
<userSettings>
<TestProject.Properties.Settings>
<setting name="Setting1" serializeAs="String">
<value>setting1</value>
</setting>
</TestProject.Properties.Settings>
<!--Set the configSource attribute for the custom section. Note that we
can't include any setting within this section-->
<MyExternalSection configSource ="commonSettings.config">
</MyExternalSection>
</userSettings>
</configuration>
5. Create a config file named 'commonSettings.config' which contains the
following content:
<?xml version="1.0" encoding="utf-8" ?>
<MyExternalSection>
<setting name="Setting2" serializeAs="String">
<value>Setting2</value>
</setting>
</MyExternalSection >
6. Add a Button on the form. In the Button's Click event handler, add the
following code to read configuration setting from the external file. You
should add a reference to System.configuration assembly to the project
first.
using System.Configuration;
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(ReadSetting("userSettings","MyExternalSection",
"Setting2"));
}
string ReadSetting(string sectionGroupName,string
sectionName,string settingName)
{
Configuration configuration =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Get sectionGroup
ConfigurationSectionGroup sectionGroup =
configuration.GetSectionGroup(sectionGroupName);
// Get section
ClientSettingsSection section =
(ClientSettingsSection)sectionGroup.Sections.Get(sectionName);
// Get setting
SettingElement setting = section.Settings.Get(settingName);
// Read setting value
return setting.Value.ValueXml.InnerText;
}
7. Build the project and run the application. Click the button on the form
and you should see a message box pops up showing the value of Setting2
stored in the commonSettings.config file.
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.