Retrieving Configuration Settings in VS2005

D

Dan

I use the settings grid to create a setting named "ServerName". Now i
want to retrieve it in code and i can't figure out how. This should be
incredibly obvious but i cant get it.

I have tried creating an AppSettingsReader and using
GetValue("ServerName", typeof(String)). That threw an error about key
not found. Then i tried ConfigurationManager.AppSettings["ServerName"]
but that returns blank. I supplied a value when i created the setting.

I did this in VB and C#. The VB one was easy. My.Settings returned
that value. The config files for both apps are the same.

What is the My.Settings equivalent in c#?

dan
 
D

Dan

Tim said:
It would be something like this...

Properties.Settings.Default.ServerName
That seems to return the default value only. If i change the value in
the config file the original value is still returned. Am i missing
something?

Dan
 
D

Dan

The value that is returned is always the value specified by the
attribute DefaultSettingValueAttribute.

The values in the config files (app.config, LibraryConfig.dll.config in
the debug directory and the one of the same name in the exe path) all
have different values.

I have a C# Class Library that gets a setting and is accessed by an vb exe.

I have been changing the values by every means possible. I have used
the VS IDE and a text editor.

I can seem to find any help on MSDN or google on this either.
 
D

Dan

That method seems to work when the application is an executeable. If i
use this with a dll it doesn't. I have changed the file.dll.config many
times but it always uses the default. Perhaps it is looking for that
value in a different config file.

dan
 
D

Dan

I think i figured it. The file.dll.config needs to be merged with the
exe's config file. why does the IDE create file.dll.config if it needs
to be merged with file.exe.config?

Very unintuitive.

dan
 
T

Tim Wilson

When you rebuild the project VS will copy the app.config file to the output
directory and rename it to [AppName.exe].config. This ensures that any
changes that you have made to the default settings through the IDE are
visible to the application when it is run. So if you change the contents of
the [AppName.exe].config file in the output directory the change(s) will be
lost next time you rebuild the application. If, however, you were to change
one of the settings through code while the application is running, assuming
that the setting is user scoped, then when the change is saved it will be
stored in a user.config file and not to the [AppName.exe].config file. For
example, if the following code was run...

Properties.Settings.Default.ServerName = "Value";
Properties.Settings.Default.Save();

.... then the new value for ServerName would be saved, by default, to a
user.config file that should be located similar to the path returned by
"System.Windows.Forms.Application.LocalUserAppDataPath".

So the end result is that the settings are loaded from the
[AppName.exe].config file if it exists, otherwise they will be pulled from
the defaults, that were specified in the source code, which were compiled
into the assembly. However, if the value was changed, and saved, through
code then the settings will be loaded from the user.config file. This allows
each user to have their own settings but still be able to resort back to
defaults if necessary. You can find out more from the MSDN help.

"Application Settings for Windows Forms"
http://msdn2.microsoft.com/en-us/library/0zszyc6e(vs.80).aspx
 
E

E J

I think I am running into the same problem. I have multiple projects in
my solution and set an application setting for one of the project which
is not the start up project. This creates an entry a file called
app.config in that project. However, when I build the solution, the
solution.exe.config file does not contain the appliction setting I
created in the other project. Do I need to merge the app config files?
If so, how do I do that?
 
K

Kevin Spencer

An application has but one app.config file. It is available to all code in
the application. So, yes, you need to put the settings for the non-startup
project into the app.config of the app project.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 

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