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

how to share settings between app <-> class lib?

P: n/a
Hello NG,

Is it possible to share the settings of an application with a class
libreary?
In my case I have a application and a set of different reports (home made)
put into a class library.
The plan is to delivere different report.dlls with the main app.

But it is essentially importent, that the reports and the app use the same
settings.
How can I achieve this?

Thanks for help and hints.

Rainer Queck
Mar 6 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a

You can look here for an idea:
http://www.eggheadcafe.com/forums/Fo...=85647&INTID=6

...

"Rainer Queck" <Ra****@noemail.noemailwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
Hello NG,

Is it possible to share the settings of an application with a class
libreary?
In my case I have a application and a set of different reports (home made)
put into a class library.
The plan is to delivere different report.dlls with the main app.

But it is essentially importent, that the reports and the app use the same
settings.
How can I achieve this?

Thanks for help and hints.

Rainer Queck


Mar 6 '07 #2

P: n/a
Hi Sloan,

thanks for the hint, but can't I just "pass on" my applications
Properties.Settings somehow?

Regards
Rainer

"sloan" <sl***@ipass.netschrieb im Newsbeitrag
news:Ou**************@TK2MSFTNGP05.phx.gbl...
>
You can look here for an idea:
http://www.eggheadcafe.com/forums/Fo...=85647&INTID=6

..

"Rainer Queck" <Ra****@noemail.noemailwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
>Hello NG,

Is it possible to share the settings of an application with a class
libreary?
In my case I have a application and a set of different reports (home
made)
put into a class library.
The plan is to delivere different report.dlls with the main app.

But it is essentially importent, that the reports and the app use the
same
settings.
How can I achieve this?

Thanks for help and hints.

Rainer Queck



Mar 6 '07 #3

P: n/a
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.

Mar 7 '07 #4

P: n/a
HI Walter,

thank you very much for the detailed answer.
It enlarged my horizont on the subject.
But since I am a very lazzy person I decided to choose option C:

I defined a interface for all needed settings and implemented it into the
classes evolved plus a little class reading my settings in the application.

Interface :
public interface IReportSettingsInterface
{
string PdfReader { get; set; }
string PdfReaderArgs { get; set; }
string ReportPath { get; set; }
string SamplePath { get; set; }
string TemplatePathe { get; set; }
}

Class constructor in library:
public GbReport(IReportSettingsInterface settings)
{
reportSettings = settings;
...
}

Settings reader class in application:
public class ReportSettings : IReportSettingsInterface
{
private string pdfReader;
private string pdfReaderArgs;
private string reportPath;
private string samplePath;
private string templatePathe;

public ReportSettings()
{
pdfReader = Properties.Settings.Default.PdfReader;
pdfReaderArgs = Properties.Settings.Default.PdfReaderArgs;
reportPath = Properties.Settings.Default.ReportPath;
samplePath = Properties.Settings.Default.SamplePath;
templatePathe = Properties.Settings.Default.TemplatePathe;
}
....
<properties set get>
}

Class factory in my app, instantiating the classes in my library:
....
GbReport report = Activator.CreateInstance(reportClassType, new Object[] {
new ReportSettings()}) as GbReport;
....

Works nice in my app ;-)

Regards
Rainer
"Walter Wang [MSFT]" <wa****@online.microsoft.comschrieb im Newsbeitrag
news:KG**************@TK2MSFTNGHUB02.phx.gbl...
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.


Mar 7 '07 #5

P: n/a
Hi Rainer,

Thank you for sharing your experience here. I'm glad you've solved the
issue.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 7 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.