App Settings for assemblies.

C

Chris Dunaway

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
 
K

Kevin Spencer

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 said:
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
 
C

Chris Dunaway

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
 
G

Guest

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("DBServerName").ToString()
sServerName = My.MySettings.Default.Item("DBServerName").ToString()
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 said:
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 said:
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
 
K

Kevin Spencer

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.
 
K

Kevin Spencer

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 said:
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("DBServerName").ToString()
sServerName = My.MySettings.Default.Item("DBServerName").ToString()
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 said:
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 said:
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
 
G

Guest

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 said:
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 said:
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("DBServerName").ToString()
sServerName = My.MySettings.Default.Item("DBServerName").ToString()
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 said:
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.

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
 
G

Guest

I figured it out. The app.config file settings can be accessed through the
System.Configuration.ConfigurationManager object.

Craig said:
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 said:
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 said:
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("DBServerName").ToString()
sServerName = My.MySettings.Default.Item("DBServerName").ToString()
sServerName = My.MySettings.Default.DBServerName

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

Thanks in advance.

:

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.

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
 
T

Thomas T. Veldhouse

In microsoft.public.dotnet.languages.csharp Craig said:
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].
 
K

Kevin Spencer

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 said:
In microsoft.public.dotnet.languages.csharp Craig
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].
 
T

Thomas T. Veldhouse

In microsoft.public.dotnet.languages.csharp Kevin Spencer said:
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].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top