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

App Settings for assemblies.

P: n/a
I have an .exe console project and a class library project as part of a
solution. The .exe has an App.config file and I have used the Settings
page to add some Application level settings.

When I try to retrieve a setting using this code:

//Method 1

string s = ConfigurationManager.AppSettings["TestSetting"];

I don't get anything back. But if I use the following code:

//Method 2

Properties.Settings _settings = new
ThrowAwayCs.Properties.Settings();
s = _settings.TestSetting;

I do get the correct value. I like the second method because I get
intellisense for all the settings. The first method seems prone to
error if I have a typo in the code because I will not know about it
until runtime.

I have two questions:

1. Is this way (method 2) of using the Settings class (which is
defined in Settings.Designer.cs) considered appropriate? It seems to
work fine.

2. I would like to use the second method in a class library project
but since the application references the class library, I cannot
reference the app from the class library because of a circular
reference. I would like to have full intellisense for my settings
inside the class libaray. Is this possible?

I would appreciate any comments on how others have used Application
Settings from a class library that is referenced by the app.

Thanks,

Chris
Keywords:
dll
app
settings
reference
intellisense
class library

Oct 2 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Hi Chris,

I understand that this is a bit confusing at first, because when you use the
Settings Page in the IDE, a namespace ("Properties") and a set of static
classes ("Settings" and "Resources") is created, which you can see in your
project via the Class Viewer.

It is actually much simpler, as each project has its own class, and if you
have a class library, it will load the Properties class from the Settings
file of the application using the Class libary. This way, each application
can have different configured Settings using the same Class Library. The
Settings class has a static Settings member named "Default" which will
return all of the Properties in the configuration file by name. Example:

AppLogFilePath = Properties.Settings.Default.AppLogFilePath;

So, for example, you can create a Class Library, with its own Settings in
the IDE, so that you will have the Intellisense while developing it, and
then create multiple apps with their own Settings (with the same names as
the Class Libary's Settings, and the Class Library will use the app's
Settings.

You can also create Application and User-Specific Settings, and the
User-Specific Settings can be saved back to the user's local Settings file
(located in Documents and Settings/userName/Local Settings/Application
Data).

See http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx for more detailed
information.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

"Chris Dunaway" <du******@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
>I have an .exe console project and a class library project as part of a
solution. The .exe has an App.config file and I have used the Settings
page to add some Application level settings.

When I try to retrieve a setting using this code:

//Method 1

string s = ConfigurationManager.AppSettings["TestSetting"];

I don't get anything back. But if I use the following code:

//Method 2

Properties.Settings _settings = new
ThrowAwayCs.Properties.Settings();
s = _settings.TestSetting;

I do get the correct value. I like the second method because I get
intellisense for all the settings. The first method seems prone to
error if I have a typo in the code because I will not know about it
until runtime.

I have two questions:

1. Is this way (method 2) of using the Settings class (which is
defined in Settings.Designer.cs) considered appropriate? It seems to
work fine.

2. I would like to use the second method in a class library project
but since the application references the class library, I cannot
reference the app from the class library because of a circular
reference. I would like to have full intellisense for my settings
inside the class libaray. Is this possible?

I would appreciate any comments on how others have used Application
Settings from a class library that is referenced by the app.

Thanks,

Chris
Keywords:
dll
app
settings
reference
intellisense
class library

Oct 2 '06 #2

P: n/a
Kevin Spencer wrote:

Thanks, you have cleared up much.
So, for example, you can create a Class Library, with its own Settings in
the IDE, so that you will have the Intellisense while developing it, and
then create multiple apps with their own Settings (with the same names as
the Class Libary's Settings, and the Class Library will use the app's
Settings.
So any settings I add to the Properties page for the class library
project, will ultimately have to be put in the MyApp.Exe.Config file,
correct? What are the best practices for getting these settings into
the config file for the app when you distribute just the class library?
Are there any methods for integrating the settings into the existing
app.config?

Also, what if the host app has a parameter with the same name as one
used by your class library? Are there any recommended ways of naming
your settings to avoid name collisions?
You can also create Application and User-Specific Settings, and the
User-Specific Settings can be saved back to the user's local Settings file
(located in Documents and Settings/userName/Local Settings/Application
Data).
I was wondering where that was!

Thanks again,

Chris

Oct 3 '06 #3

P: n/a
Kevin -

I'm trying to do this in VB.NET and I'm unable to get it working. I've tried
the following variations; the first two show me the proper setting in
IntelliSense, but throw an exception at runtime ("Object reference not set to
an instance of an object."). The third one doesn't compile as DBServerName is
not a recognized member of Default:
sServerName =
My.MySettings.Default.Properties.Item("DBServerNam e").ToString()
sServerName = My.MySettings.Default.Item("DBServerName").ToStrin g()
sServerName = My.MySettings.Default.DBServerName

Is there something more I need to do to expose application settings to the
assemblies?

Thanks in advance.

"Kevin Spencer" wrote:
Hi Chris,

I understand that this is a bit confusing at first, because when you use the
Settings Page in the IDE, a namespace ("Properties") and a set of static
classes ("Settings" and "Resources") is created, which you can see in your
project via the Class Viewer.

It is actually much simpler, as each project has its own class, and if you
have a class library, it will load the Properties class from the Settings
file of the application using the Class libary. This way, each application
can have different configured Settings using the same Class Library. The
Settings class has a static Settings member named "Default" which will
return all of the Properties in the configuration file by name. Example:

AppLogFilePath = Properties.Settings.Default.AppLogFilePath;

So, for example, you can create a Class Library, with its own Settings in
the IDE, so that you will have the Intellisense while developing it, and
then create multiple apps with their own Settings (with the same names as
the Class Libary's Settings, and the Class Library will use the app's
Settings.

You can also create Application and User-Specific Settings, and the
User-Specific Settings can be saved back to the user's local Settings file
(located in Documents and Settings/userName/Local Settings/Application
Data).

See http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx for more detailed
information.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

"Chris Dunaway" <du******@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
I have an .exe console project and a class library project as part of a
solution. The .exe has an App.config file and I have used the Settings
page to add some Application level settings.

When I try to retrieve a setting using this code:

//Method 1

string s = ConfigurationManager.AppSettings["TestSetting"];

I don't get anything back. But if I use the following code:

//Method 2

Properties.Settings _settings = new
ThrowAwayCs.Properties.Settings();
s = _settings.TestSetting;

I do get the correct value. I like the second method because I get
intellisense for all the settings. The first method seems prone to
error if I have a typo in the code because I will not know about it
until runtime.

I have two questions:

1. Is this way (method 2) of using the Settings class (which is
defined in Settings.Designer.cs) considered appropriate? It seems to
work fine.

2. I would like to use the second method in a class library project
but since the application references the class library, I cannot
reference the app from the class library because of a circular
reference. I would like to have full intellisense for my settings
inside the class libaray. Is this possible?

I would appreciate any comments on how others have used Application
Settings from a class library that is referenced by the app.

Thanks,

Chris
Keywords:
dll
app
settings
reference
intellisense
class library


Oct 3 '06 #4

P: n/a
Hi Chris,
So any settings I add to the Properties page for the class library
project, will ultimately have to be put in the MyApp.Exe.Config file,
correct? What are the best practices for getting these settings into
the config file for the app when you distribute just the class library?
Are there any methods for integrating the settings into the existing
app.config?
Yes, sort of. You would add them via the Properties page for your
application, thereby re-creating the exact same class configuration. Don't
edit the app.config file if you do it this way. Besides, it's easier to use
the Properties page. As for the best practices, there are a couple of
suggestions I can make:

1. Define default values in your code for the class library, using a field
initializer. Example:

private string _FileLocation = "\\";
public string FileLocation { get { return _FileLocation; } }

In some cases, a default will make sense. Usually defaults do.
At the least, the field will have a value. If the developer omits the
value in the
project, at least the value will not be null.
If the assembly is kept in the GAC, modifying the machine.config file
might be in order.
2. Good Exception Handling. (nuff said, I think)
3. Good Documentation. Inform the developer of the default and the config
value
necessary to use the field/property.
Also, what if the host app has a parameter with the same name as one
used by your class library? Are there any recommended ways of naming
your settings to avoid name collisions?
Assuming that the developer using the class library has control over his/her
project (a good assumption), that is the developer's job to do. It isn't
likely to build if not! As for recommended naming conventions, I would think
that adding the namespace or class name to the name of the configuration
item might be a good idea, such as "ThisLibray_ConnectionString". Of course,
again, it isn't likely to happen, but I can see that you are a diligent
developer, so, use your own convention, or mine, if you like.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

If the Truth hurts, wear it.


Oct 3 '06 #5

P: n/a
Hi Craig,

I don't use VB, so I'm only familiar with the CLR syntax. However, I can
tell you is a couple of things, I think. First, the Settings class is not a
Collection. Each Setting is a strongly-typed property of the class. Second,
that the Collection is strongly-typed, and you don't need to do any casting
or converting.

You should read the following:
http://msdn2.microsoft.com/en-us/library/bc6ws923.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

If the Truth hurts, wear it.

"Craig" <CraigM@REMOVE_CAPS_AND_INVALIDco.summit.co.us.inv alidwrote in
message news:48**********************************@microsof t.com...
Kevin -

I'm trying to do this in VB.NET and I'm unable to get it working. I've
tried
the following variations; the first two show me the proper setting in
IntelliSense, but throw an exception at runtime ("Object reference not set
to
an instance of an object."). The third one doesn't compile as DBServerName
is
not a recognized member of Default:
sServerName =
My.MySettings.Default.Properties.Item("DBServerNam e").ToString()
sServerName = My.MySettings.Default.Item("DBServerName").ToStrin g()
sServerName = My.MySettings.Default.DBServerName

Is there something more I need to do to expose application settings to the
assemblies?

Thanks in advance.

"Kevin Spencer" wrote:
>Hi Chris,

I understand that this is a bit confusing at first, because when you use
the
Settings Page in the IDE, a namespace ("Properties") and a set of static
classes ("Settings" and "Resources") is created, which you can see in
your
project via the Class Viewer.

It is actually much simpler, as each project has its own class, and if
you
have a class library, it will load the Properties class from the Settings
file of the application using the Class libary. This way, each
application
can have different configured Settings using the same Class Library. The
Settings class has a static Settings member named "Default" which will
return all of the Properties in the configuration file by name. Example:

AppLogFilePath = Properties.Settings.Default.AppLogFilePath;

So, for example, you can create a Class Library, with its own Settings in
the IDE, so that you will have the Intellisense while developing it, and
then create multiple apps with their own Settings (with the same names as
the Class Libary's Settings, and the Class Library will use the app's
Settings.

You can also create Application and User-Specific Settings, and the
User-Specific Settings can be saved back to the user's local Settings
file
(located in Documents and Settings/userName/Local Settings/Application
Data).

See http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx for more
detailed
information.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

"Chris Dunaway" <du******@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegr oups.com...
>I have an .exe console project and a class library project as part of a
solution. The .exe has an App.config file and I have used the Settings
page to add some Application level settings.

When I try to retrieve a setting using this code:

//Method 1

string s = ConfigurationManager.AppSettings["TestSetting"];

I don't get anything back. But if I use the following code:

//Method 2

Properties.Settings _settings = new
ThrowAwayCs.Properties.Settings();
s = _settings.TestSetting;

I do get the correct value. I like the second method because I get
intellisense for all the settings. The first method seems prone to
error if I have a typo in the code because I will not know about it
until runtime.

I have two questions:

1. Is this way (method 2) of using the Settings class (which is
defined in Settings.Designer.cs) considered appropriate? It seems to
work fine.

2. I would like to use the second method in a class library project
but since the application references the class library, I cannot
reference the app from the class library because of a circular
reference. I would like to have full intellisense for my settings
inside the class libaray. Is this possible?

I would appreciate any comments on how others have used Application
Settings from a class library that is referenced by the app.

Thanks,

Chris
Keywords:
dll
app
settings
reference
intellisense
class library



Oct 3 '06 #6

P: n/a
OK. Anyone else care to chime in with some VB.NET tips? I've been beating my
head against the wall for a couple days trying to access app settings from
within assemblies that are in separate projects. I've looked through MSDN and
struck out (so far). This seems like it ought to be a simple matter, but I
sure haven't been able to get the right combination.

Thanks,
Craig.

"Kevin Spencer" wrote:
Hi Craig,

I don't use VB, so I'm only familiar with the CLR syntax. However, I can
tell you is a couple of things, I think. First, the Settings class is not a
Collection. Each Setting is a strongly-typed property of the class. Second,
that the Collection is strongly-typed, and you don't need to do any casting
or converting.

You should read the following:
http://msdn2.microsoft.com/en-us/library/bc6ws923.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

If the Truth hurts, wear it.

"Craig" <CraigM@REMOVE_CAPS_AND_INVALIDco.summit.co.us.inv alidwrote in
message news:48**********************************@microsof t.com...
Kevin -

I'm trying to do this in VB.NET and I'm unable to get it working. I've
tried
the following variations; the first two show me the proper setting in
IntelliSense, but throw an exception at runtime ("Object reference not set
to
an instance of an object."). The third one doesn't compile as DBServerName
is
not a recognized member of Default:
sServerName =
My.MySettings.Default.Properties.Item("DBServerNam e").ToString()
sServerName = My.MySettings.Default.Item("DBServerName").ToStrin g()
sServerName = My.MySettings.Default.DBServerName

Is there something more I need to do to expose application settings to the
assemblies?

Thanks in advance.

"Kevin Spencer" wrote:
Hi Chris,

I understand that this is a bit confusing at first, because when you use
the
Settings Page in the IDE, a namespace ("Properties") and a set of static
classes ("Settings" and "Resources") is created, which you can see in
your
project via the Class Viewer.

It is actually much simpler, as each project has its own class, and if
you
have a class library, it will load the Properties class from the Settings
file of the application using the Class libary. This way, each
application
can have different configured Settings using the same Class Library. The
Settings class has a static Settings member named "Default" which will
return all of the Properties in the configuration file by name. Example:

AppLogFilePath = Properties.Settings.Default.AppLogFilePath;

So, for example, you can create a Class Library, with its own Settings in
the IDE, so that you will have the Intellisense while developing it, and
then create multiple apps with their own Settings (with the same names as
the Class Libary's Settings, and the Class Library will use the app's
Settings.

You can also create Application and User-Specific Settings, and the
User-Specific Settings can be saved back to the user's local Settings
file
(located in Documents and Settings/userName/Local Settings/Application
Data).

See http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx for more
detailed
information.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

"Chris Dunaway" <du******@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
I have an .exe console project and a class library project as part of a
solution. The .exe has an App.config file and I have used the Settings
page to add some Application level settings.

When I try to retrieve a setting using this code:

//Method 1

string s = ConfigurationManager.AppSettings["TestSetting"];

I don't get anything back. But if I use the following code:

//Method 2

Properties.Settings _settings = new
ThrowAwayCs.Properties.Settings();
s = _settings.TestSetting;

I do get the correct value. I like the second method because I get
intellisense for all the settings. The first method seems prone to
error if I have a typo in the code because I will not know about it
until runtime.

I have two questions:

1. Is this way (method 2) of using the Settings class (which is
defined in Settings.Designer.cs) considered appropriate? It seems to
work fine.

2. I would like to use the second method in a class library project
but since the application references the class library, I cannot
reference the app from the class library because of a circular
reference. I would like to have full intellisense for my settings
inside the class libaray. Is this possible?

I would appreciate any comments on how others have used Application
Settings from a class library that is referenced by the app.

Thanks,

Chris
Keywords:
dll
app
settings
reference
intellisense
class library



Oct 3 '06 #7

P: n/a
I figured it out. The app.config file settings can be accessed through the
System.Configuration.ConfigurationManager object.

"Craig" wrote:
OK. Anyone else care to chime in with some VB.NET tips? I've been beating my
head against the wall for a couple days trying to access app settings from
within assemblies that are in separate projects. I've looked through MSDN and
struck out (so far). This seems like it ought to be a simple matter, but I
sure haven't been able to get the right combination.

Thanks,
Craig.

"Kevin Spencer" wrote:
Hi Craig,

I don't use VB, so I'm only familiar with the CLR syntax. However, I can
tell you is a couple of things, I think. First, the Settings class is not a
Collection. Each Setting is a strongly-typed property of the class. Second,
that the Collection is strongly-typed, and you don't need to do any casting
or converting.

You should read the following:
http://msdn2.microsoft.com/en-us/library/bc6ws923.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

If the Truth hurts, wear it.

"Craig" <CraigM@REMOVE_CAPS_AND_INVALIDco.summit.co.us.inv alidwrote in
message news:48**********************************@microsof t.com...
Kevin -
>
I'm trying to do this in VB.NET and I'm unable to get it working. I've
tried
the following variations; the first two show me the proper setting in
IntelliSense, but throw an exception at runtime ("Object reference not set
to
an instance of an object."). The third one doesn't compile as DBServerName
is
not a recognized member of Default:
sServerName =
My.MySettings.Default.Properties.Item("DBServerNam e").ToString()
sServerName = My.MySettings.Default.Item("DBServerName").ToStrin g()
sServerName = My.MySettings.Default.DBServerName
>
Is there something more I need to do to expose application settings to the
assemblies?
>
Thanks in advance.
>
"Kevin Spencer" wrote:
>
>Hi Chris,
>>
>I understand that this is a bit confusing at first, because when you use
>the
>Settings Page in the IDE, a namespace ("Properties") and a set of static
>classes ("Settings" and "Resources") is created, which you can see in
>your
>project via the Class Viewer.
>>
>It is actually much simpler, as each project has its own class, and if
>you
>have a class library, it will load the Properties class from the Settings
>file of the application using the Class libary. This way, each
>application
>can have different configured Settings using the same Class Library. The
>Settings class has a static Settings member named "Default" which will
>return all of the Properties in the configuration file by name. Example:
>>
>AppLogFilePath = Properties.Settings.Default.AppLogFilePath;
>>
>So, for example, you can create a Class Library, with its own Settings in
>the IDE, so that you will have the Intellisense while developing it, and
>then create multiple apps with their own Settings (with the same names as
>the Class Libary's Settings, and the Class Library will use the app's
>Settings.
>>
>You can also create Application and User-Specific Settings, and the
>User-Specific Settings can be saved back to the user's local Settings
>file
>(located in Documents and Settings/userName/Local Settings/Application
>Data).
>>
>See http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx for more
>detailed
>information.
>>
>--
>HTH,
>>
>Kevin Spencer
>Microsoft MVP
>Software Composer
>http://unclechutney.blogspot.com
>>
>A watched clock never boils.
>>
>"Chris Dunaway" <du******@gmail.comwrote in message
>news:11*********************@m73g2000cwd.googlegr oups.com...
>I have an .exe console project and a class library project as part of a
solution. The .exe has an App.config file and I have used the Settings
page to add some Application level settings.
>
When I try to retrieve a setting using this code:
>
//Method 1
>
string s = ConfigurationManager.AppSettings["TestSetting"];
>
I don't get anything back. But if I use the following code:
>
//Method 2
>
Properties.Settings _settings = new
ThrowAwayCs.Properties.Settings();
s = _settings.TestSetting;
>
I do get the correct value. I like the second method because I get
intellisense for all the settings. The first method seems prone to
error if I have a typo in the code because I will not know about it
until runtime.
>
I have two questions:
>
1. Is this way (method 2) of using the Settings class (which is
defined in Settings.Designer.cs) considered appropriate? It seems to
work fine.
>
2. I would like to use the second method in a class library project
but since the application references the class library, I cannot
reference the app from the class library because of a circular
reference. I would like to have full intellisense for my settings
inside the class libaray. Is this possible?
>
I would appreciate any comments on how others have used Application
Settings from a class library that is referenced by the app.
>
Thanks,
>
Chris
>
>
Keywords:
dll
app
settings
reference
intellisense
class library
>
>>
>>
>>
Oct 3 '06 #8

P: n/a
In microsoft.public.dotnet.languages.csharp Craig <CraigM@remove_caps_and_invalidco.summit.co.us.inv alidwrote:
I figured it out. The app.config file settings can be accessed through the
System.Configuration.ConfigurationManager object.
This is far different than what others have been talking to you about. Too
bad you didn't mention App.Config in the first place. In any event, this only
works directly on the App.Config associated with the executable program (i.e.
Test.exe will use Test.exe.config), and each DLL assembly does not get its
only configuration [I wish there was a simple way to make that work].

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Oct 3 '06 #9

P: n/a
In any event, this only
works directly on the App.Config associated with the executable program
(i.e.
Test.exe will use Test.exe.config), and each DLL assembly does not get its
only configuration [I wish there was a simple way to make that work].
The app.config file is used by all assemblies in the application domain, so
it can be used by any class libraries used by the application.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

If the Truth hurts, wear it.

"Thomas T. Veldhouse" <ve*****@yahoo.comwrote in message
news:ld*****************@textfe.usenetserver.com.. .
In microsoft.public.dotnet.languages.csharp Craig
<CraigM@remove_caps_and_invalidco.summit.co.us.inv alidwrote:
>I figured it out. The app.config file settings can be accessed through
the
System.Configuration.ConfigurationManager object.

This is far different than what others have been talking to you about.
Too
bad you didn't mention App.Config in the first place. In any event, this
only
works directly on the App.Config associated with the executable program
(i.e.
Test.exe will use Test.exe.config), and each DLL assembly does not get its
only configuration [I wish there was a simple way to make that work].

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1


Oct 4 '06 #10

P: n/a
In microsoft.public.dotnet.languages.csharp Kevin Spencer <uc*@ftc.govwrote:
In any event, this only
>works directly on the App.Config associated with the executable program
(i.e.
Test.exe will use Test.exe.config), and each DLL assembly does not get its
only configuration [I wish there was a simple way to make that work].

The app.config file is used by all assemblies in the application domain, so
it can be used by any class libraries used by the application.
Yes, but there have been many circumstance where I wish an assembly could have
its OWN App.Config. It gets tedious maintaining App.Config files for several
consumers of a given library [assembly].

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Oct 4 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.