Please help me figure out ConfigurationManager.AppSettings for aWindows Forms .NET 2.0 project

J

jman

I am trying to understand ConfigurationManager and why I cannot get
any of my Application Settings using
ConfigurationManager.AppSettings["MySetting"]. I am not having
compiler/build errors, its just that I am not getting my application's
settings/values. Can anyone help me?

I create a new C# Windows Forms project in VS2005 for .NET 2.0
Include reference to System.Configuration under references
Go to Project -> Properties, then the "Settings" side-tab
Add a string setting "MySetting" (Scope = Application) with value
"test"

Then I add some source code to the constructor for the default
"Form1":
using System.Configuration ( near the top )
string s1 = WindowsApplication1.Properties.Settings.Default.MySetting;
string s2 = ConfigurationManager.AppSettings["MySetting"];

Now, when stepping under the debugger I would expect that s1 == s2,
but in fact s1 is "test" (expected) and s2 is null (unexpected).

What am I doing wrong? How can I use ConfigurationManager to read the
string value of the application-scope setting MySetting?
 
A

Alex Meleta

Hi jman,

ConfigurationManager.AppSettings it's for <appSettings> of configuration
which is not for your case. There are two sometimes ambiguous sections in
the configuration file - appSettings and applicationSettings. First one came
from early releases, the second one is a replacement. Following article explains
you a difference http://www.devx.com/dotnet/Article/34273/1954

Regards, Alex
 
J

jman

Interesting read, thanks for the link Alex!

I played around with this a little more and I discovered some
interesting behavior with ConnectionStrings:
Same basic Windows Form project from before, all references
(System.Configuration, etc.) and I added a simple ConnectionString
("MyConnStr") via the Settings Editor GUI. I found the following:

string s3 = WindowsApplication1.Properties.Settings.Default.MyConnStr;
string s4 =
ConfigurationManager.ConnectionStrings["WindowsApplication1.Properties.Settings.MyConnStr"].ConnectionString;
string s5 =
ConfigurationManager.ConnectionStrings["MyConnStr"].ConnectionString;
Now, s3 and s4 both contain my connection string correctly, but s5
throws an exception (because I used "MyConnStr" instead of the fully
qualified(?) string "WindowsApplicaion1.Prop...MyConnStr".

So it seems that ConfigurationManager has access to my
ConnectionString but not the normal string under ApplicationSettings.
When I break with the debugger and inspect ConfigurationManager by
nosing through the QuickWatch but I cannot see a trace of the
MySetting (Application) setting. It seems that ConfigurationManager
simply ignores that part of the config XML entirely. Its too bad
because I would have liked the alternate access even if it cost me the
strong typing.
 
A

Alex Meleta

Hi jman,

j> So it seems that ConfigurationManager has access to my
j> ConnectionString but not the normal string under ApplicationSettings.
j> When I break with the debugger and inspect ConfigurationManager by
j> nosing through the QuickWatch but I cannot see a trace of the
j> MySetting (Application) setting. It seems that ConfigurationManager
j> simply ignores that part of the config XML entirely. Its too bad
j> because I would have liked the alternate access even if it cost me
j> the strong typing.

If you mean that ConfigurationManager doesn't read the <applicationSettings>
section, then, yes. That's right, because, basicly, ConfigurationManager.AppSettings
and .ConnectionStringsi are something like

public static NameValueCollection AppSettings { get
{ return (NameValueCollection) GetSection("appSettings"); }}

public static ConnectionStringSettingsCollection ConnectionStrings{ get
{ ConnectionStringsSection section = (ConnectionStringsSection) GetSection("connectionStrings");
return section.ConnectionStrings; }}

Regards, Alex
 
G

gerry

sorry, I only caught the trail end of this post , not sure if this answers
the question :

applications can retrieve values from app.config appSettings section using :
string value = System.Configuration.ConfigurationManager.AppSettings [
"SettingName" ];
 
G

gerry

I located the entire thread, using the Settings.settings doesn't work as you
discovered.
This will work if you manually edit the app.config <appSetting> section.
<configuration>

<appSettings>
<add key="SettingName" value="setting value" />
</appSettings>
</configuration>



gerry said:
sorry, I only caught the trail end of this post , not sure if this answers
the question :

applications can retrieve values from app.config appSettings section using
:
string value = System.Configuration.ConfigurationManager.AppSettings
[ "SettingName" ];



Alex Meleta said:
Hi jman,

j> So it seems that ConfigurationManager has access to my
j> ConnectionString but not the normal string under ApplicationSettings.
j> When I break with the debugger and inspect ConfigurationManager by
j> nosing through the QuickWatch but I cannot see a trace of the
j> MySetting (Application) setting. It seems that ConfigurationManager
j> simply ignores that part of the config XML entirely. Its too bad
j> because I would have liked the alternate access even if it cost me
j> the strong typing.

If you mean that ConfigurationManager doesn't read the
<applicationSettings> section, then, yes. That's right, because, basicly,
ConfigurationManager.AppSettings and .ConnectionStringsi are something
like

public static NameValueCollection AppSettings { get
{ return (NameValueCollection) GetSection("appSettings"); }}

public static ConnectionStringSettingsCollection ConnectionStrings{ get
{ ConnectionStringsSection section = (ConnectionStringsSection)
GetSection("connectionStrings");
return section.ConnectionStrings; }}

Regards, Alex
 

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