Cannot read in the keys from app.config (.NET 2.0)

  • Thread starter Thread starter Project2501a
  • Start date Start date
P

Project2501a

Hey guys,

I'm stumped! :( I've been trying now for the last four days to read in
the settings from an app.config file, but i've failed miserably, at
what seemed like a simple task.

I was trying to find out how the whole System.Configuration namespace
is working. So, I made a simple C# windows application: One form, with
a single textbox and and a simple "Load" button. this is what's in the
Event handler:


private void cmdLoad_Click( object sender, EventArgs e )
{
// txtSettings is a textbox
txtSettings.Text =
System.Configuration.ConfigurationManager.AppSettings["MySetting"];

}

BUT! when i click on the load button, the textbox does not get
assigned any value! i mean, nada, zilttz, String.Empy, "" . I set up a
breakpoing on the assignment above and i examined AppSettings[] . Turns
out the AllKeys member has 0 (zero) dimensions. I was flabergasted :(

Following is my App.settings file. I added this file by right clicking
on the solution explorer, on the Project name (not the solution name)
and then selecting "add new item ->
Application Configuration file.

Then, i went into Project Properties, clicked on the "Settings" tab and
added three key/value pairs: MySetting, MyOtherSetting and
MyOtherSetting. all three are at application level.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ConfigurationExample.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<ConfigurationExample.Properties.Settings>
<setting name="MySetting" serializeAs="String">
<value>HUHU</value>
</setting>
<setting name="MyOtherSetting" serializeAs="String">
<value>Tzimako alaka!</value>
</setting>
<setting name="MyLastSetting" serializeAs="String">
<value>ALL YOUR BASE</value>
</setting>
</ConfigurationExample.Properties.Settings>
</applicationSettings>
<startup>
<supportedRuntime version="v2.0.50727" />
</startup>
</configuration>

Here are some questions:

i've seen plenty of 1.1 example which ALL use appSettigns instead of
applicationSettings. Keep in mind this is a 2.0 application file. And
yes, appSettings WORKS GREAT if i add that section in manually.

1) is the <appSettings> section still required by the 2.0 framework in
order to read in application settings data?

1.1) if yes, why isn't it explicitely created or why doesn't the
preprocessor/compiler throw a warning about it?

1.2) Yes, i could use the <appSettings> section and do my job
worderfully and get this over with. But that's not the point: If 2.0
has it's own way of retrieving application settings data, i should be
able to use *THAT* instead of the depresiated 1.1 way, shouldn't I?
Where am I ****ing up?


2) requirePermission="false" <-- what does this setting do?
2.1) why does VS2k5 bitch about it not being declared?
2.2) do I need to declare it?
2.2.1) if yes, where and how?

thanks guys, looking forward to your answers.

George Marselis
 
Have a look in the 'Settings.Designer.cs' file generated in your app to
get a better idea of what is going on - meanwhile try

txtSettings.Text = {your
namespace}.Properties.Settings.Default.MySetting;

Have fun
Howard.
 
hey Howard, thanks for the reply. Works great! but isn't this kinda of
lame? i would like to know why my set up doesn't work
 
hey Howard, thanks for the reply. Works great! but isn't this way,
kinda of lame? i would like to know why my set up doesn't work
 
I'd much rather have a named method that would fail at compile time,
that a string indexer that might fail at runtime!!
H
 
Back
Top