Hi Rainer,
You will have two options:
Option A: Use SettingsGroupNameAttribute to specify a section name in your
class library, so that it could use the same settings section in your
application config (which is also used by your application):
1) In your class library, create the settings as usual; you could then use
strong-typed Settings.Default.<Keyto access those settings.
2) Create another separate class:
namespace ClassLibrary1.Properties
{
[SettingsGroupName("ConsoleApplication1.Properties. Settings")]
internal sealed partial class Settings :
global::System.Configuration.ApplicationSettingsBa se
{
}
}
(You need to add reference to System.Configuration.dll)
Please note this "ConsoleApplication1.Properties.Settings" can be modified,
just remember to use the settings section name in the application config
file (see below).
Also, the namespace should be same to the designer generated
Settings.Design.cs.
3) In your application, also use the settings designer to create those
settings (or simply copy over the content from app.config). Open the
app.config, you will see the
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGrou p, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ConsoleApplication1.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<ConsoleApplication1.Properties.Settings>
<setting name="Key1" serializeAs="String">
<value>ConsoleApplication1</value>
</setting>
</ConsoleApplication1.Properties.Settings>
</applicationSettings>
If you changed the section name "ConsoleApplication1.Properties.Settings",
make sure you also modify them here.
4) Now reference the class library from the application, the class library
now will use the application's settings as well.
Option B: use "configSource" attribute of config section to move both the
class library and the application's settings into a separate settings file:
1) Create the settings in the class library as usual, add some settings;
then open app.config, copy the settings section
"ClassLibrary1.Properties.Settings" and save it into a separate file, for
example "SharedSettings.xml":
<ClassLibrary1.Properties.Settings>
<setting name="Key1" serializeAs="String">
<value>ClassLibrary1</value>
</setting>
</ClassLibrary1.Properties.Settings>
Modify the app.config to remove the content of the settings section and add
"configSource" attribute to point it to the external file
"SharedSettings.xml":
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGrou p, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ClassLibrary1.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<ClassLibrary1.Properties.Settings
configSource="SharedSettings.xml">
</ClassLibrary1.Properties.Settings>
</applicationSettings>
2) In your application's config file, also point its settings section to
use this file:
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGrou p, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ConsoleApplication2.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<ConsoleApplication2.Properties.Settings
configSource="SharedSettings.xml">
</ConsoleApplication2.Properties.Settings>
</applicationSettings>
3) Copy the SharedSettings.xml from the class library to your application
directory, make sure you set its "Copy to output directory" property to
"Copy always" in its properties window.
4) Now both your class library and your application are using the separate
settings xml file "SharedSettings.xml".
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, 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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.