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

Yet another app.config question - Isn't this supposed to be easy?

P: n/a
Hello all,

I have a C# Windows Forms app. It is in namespace App.GUI. It builds to Nav.exe. I have entered an application level setting using the designer. Its type is string, name is "FOO" and value is "monkey". I've tried the following ways to retrieve the value and only one works:

//Returns null
Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly .GetEntryAssembly().Location);
string monkey = config.AppSettings.Settings["FOO"];

//Returns null - I know this is deprecated
string monkey = ConfigurationSettings.AppSettings["FOO"];

//Returns null
string monkey = ConfigurationManager.AppSettings["FOO"];

//Returns correct value
string monkey = global::App.GUI.Properties.Settings.Default["FOO"].ToString();

The Nav.exe.config file is in the right place. The Configuration object (first example) contains the correct path and its HasFile property is True. A Label bound to FOO displays the correct value. Looking at the XML in Nav.exe.config shows

<applicationSettings>
<App.GUI.Properties.Settings>
<setting name="FOO" serializeAs="String">
<value>monkey</value>
</setting>
</App.GUI.Properties.Settings>
</applicationSettings>

So what am I doing wrong? Using global:: isn't very helpful when trying to get at these settings from another assembly (BTW all the methods that fail in the .exe fail identically in satellite assemblies)

Any helpful advice or suggestions are greatly appreciated.

Keith
Sep 20 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You should just be able to write
Properties.Settings.Default["FOO"].ToString();

Ciaran O'Donnell

"Keith" wrote:
Hello all,

I have a C# Windows Forms app. It is in namespace App.GUI. It builds to Nav.exe. I have entered an application level setting using the designer. Its type is string, name is "FOO" and value is "monkey". I've tried the following ways to retrieve the value and only one works:

//Returns null
Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly .GetEntryAssembly().Location);
string monkey = config.AppSettings.Settings["FOO"];

//Returns null - I know this is deprecated
string monkey = ConfigurationSettings.AppSettings["FOO"];

//Returns null
string monkey = ConfigurationManager.AppSettings["FOO"];

//Returns correct value
string monkey = global::App.GUI.Properties.Settings.Default["FOO"].ToString();

The Nav.exe.config file is in the right place. The Configuration object (first example) contains the correct path and its HasFile property is True. A Label bound to FOO displays the correct value. Looking at the XML in Nav.exe.config shows

<applicationSettings>
<App.GUI.Properties.Settings>
<setting name="FOO" serializeAs="String">
<value>monkey</value>
</setting>
</App.GUI.Properties.Settings>
</applicationSettings>

So what am I doing wrong? Using global:: isn't very helpful when trying to get at these settings from another assembly (BTW all the methods that fail in the .exe fail identically in satellite assemblies)

Any helpful advice or suggestions are greatly appreciated
Sep 21 '06 #2

P: n/a
Ciaran O''Donnell wrote:
You should just be able to write
Properties.Settings.Default["FOO"].ToString();
I believe that this always returns the *default* value and not
necessarily the value in the file. Look at the code behind file for
the settings. As you add settings, it inserts new properties to the
Settings class so you can declare an instance of this class and use it
for your settings. One benefit is that you don't specify your setting
using a string literal like "FOO" so you reduce the chance for a typo.

What we have done is something like this:

using App.UI.Properties;

Settings _settings = new Settings();
string value = _settings.FOO;

I'm not 100% sure this is necessary, but it works.

I have a C# Windows Forms app. It is in namespace App.GUI. It builds to Nav.exe. I have entered an application level setting using the designer. Its type is string, name is "FOO" and value is "monkey". I've tried the following ways to retrieve the value and only one works:

//Returns null
Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly .GetEntryAssembly().Location);
string monkey = config.AppSettings.Settings["FOO"];

//Returns null - I know this is deprecated
string monkey = ConfigurationSettings.AppSettings["FOO"];

//Returns null
string monkey = ConfigurationManager.AppSettings["FOO"];

//Returns correct value
string monkey = global::App.GUI.Properties.Settings.Default["FOO"].ToString();

The Nav.exe.config file is in the right place. The Configuration object (first example) contains the correct path and its HasFile property is True. A Label bound to FOO displays the correct value. Looking at the XML in Nav.exe.config shows

<applicationSettings>
<App.GUI.Properties.Settings>
<setting name="FOO" serializeAs="String">
<value>monkey</value>
</setting>
</App.GUI.Properties.Settings>
</applicationSettings>

So what am I doing wrong? Using global:: isn't very helpful when trying to get at these settings from another assembly (BTW all the methods that fail in the .exe fail identically in satellite assemblies)

Any helpful advice or suggestions are greatly appreciated
Sep 21 '06 #3

P: n/a
I appreciate the responses, however there seems to be something essential
I'm missing. Why don't the methods for loading config information directly
from the app.settings file actually work? All the examples on the net
simply load the file and get the settings by name and off they go. This
does NOT work for me in any way.

I can get settings via Properties.Settings in the project for the main
assembly, but what about satellites? Unless I pass in the settings
collection (or something similar) that information isn't available to them
since loading from the app.config doesn't work. They don't even work for
for the main assembly!

Has anyone actually used the ConfigurationManager successfully? How about
retrieving app.config information from a satellite assembly? MVP's I'd
appreciate some clarification here.

Thanks!

"Chris Dunaway" <du******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Ciaran O''Donnell wrote:
>You should just be able to write
Properties.Settings.Default["FOO"].ToString();

I believe that this always returns the *default* value and not
necessarily the value in the file. Look at the code behind file for
the settings. As you add settings, it inserts new properties to the
Settings class so you can declare an instance of this class and use it
for your settings. One benefit is that you don't specify your setting
using a string literal like "FOO" so you reduce the chance for a typo.

What we have done is something like this:

using App.UI.Properties;

Settings _settings = new Settings();
string value = _settings.FOO;

I'm not 100% sure this is necessary, but it works.

I have a C# Windows Forms app. It is in namespace App.GUI. It builds
to Nav.exe. I have entered an application level setting using the
designer. Its type is string, name is "FOO" and value is "monkey".
I've tried the following ways to retrieve the value and only one works:

//Returns null
Configuration config =
ConfigurationManager.OpenExeConfiguration(Assembly .GetEntryAssembly().Location);
string monkey = config.AppSettings.Settings["FOO"];

//Returns null - I know this is deprecated
string monkey = ConfigurationSettings.AppSettings["FOO"];

//Returns null
string monkey = ConfigurationManager.AppSettings["FOO"];

//Returns correct value
string monkey =
global::App.GUI.Properties.Settings.Default["FOO"].ToString();

The Nav.exe.config file is in the right place. The Configuration
object (first example) contains the correct path and its HasFile
property is True. A Label bound to FOO displays the correct value.
Looking at the XML in Nav.exe.config shows

<applicationSettings>
<App.GUI.Properties.Settings>
<setting name="FOO" serializeAs="String">
<value>monkey</value>
</setting>
</App.GUI.Properties.Settings>
</applicationSettings>

So what am I doing wrong? Using global:: isn't very helpful when
trying to get at these settings from another assembly (BTW all the
methods that fail in the .exe fail identically in satellite assemblies)

Any helpful advice or suggestions are greatly appreciated

Sep 21 '06 #4

P: n/a
hello Keith,
I've discovered something that is almost certainly a bug in
ConfigurationManager, and is probably what you are running into.

Try this test:
create a config file with a simple key/value setting in it, and store
the file in c:\temp\configtest.exe.config.

then run

Configuration config =
OpenExeConfiguration("c:\\temp\configtest.exe.conf ig");
Console.WriteLine(config.FilePath);

You'll see that the filename is "c:\temp\configtest.exe.config.config".
Note the extra "config" extension! That file of course does not
exist, so the settings all come up empty.

OpenExeConfiguration checks for the existence of the file you specify,
so you can't work around this by coding
"OpenExeConfig("c:\temp\configtest.exe")

However, there is a clumsy workaround. Create 2 configuration files,
one named "configtest.exe.config" and the second named
"configtest.exe.config.config". The first one allows the OpenExeConfig
to succeed, and the second one is actually used for reading the values.
Odd one, huh? I just discovered this today. Think I'll make a
separate posting.
- Leo


Keith wrote:
Hello all,

I have a C# Windows Forms app. It is in namespace App.GUI. It builds to Nav.exe. I have entered an application level setting using the designer. Its type is string, name is "FOO" and value is "monkey". I've tried the following ways to retrieve the value and only one works:

//Returns null
Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly .GetEntryAssembly().Location);
string monkey = config.AppSettings.Settings["FOO"];

//Returns null - I know this is deprecated
string monkey = ConfigurationSettings.AppSettings["FOO"];

//Returns null
string monkey = ConfigurationManager.AppSettings["FOO"];

//Returns correct value
string monkey = global::App.GUI.Properties.Settings.Default["FOO"].ToString();

The Nav.exe.config file is in the right place. The Configuration object (first example) contains the correct path and its HasFile property is True. A Label bound to FOO displays the correct value. Looking at the XML in Nav.exe.config shows

<applicationSettings>
<App.GUI.Properties.Settings>
<setting name="FOO" serializeAs="String">
<value>monkey</value>
</setting>
</App.GUI.Properties.Settings>
</applicationSettings>

So what am I doing wrong? Using global:: isn't very helpful when trying to get at these settings from another assembly (BTW all the methods that fail in the .exe fail identically in satellite assemblies)

Any helpful advice or suggestions are greatly appreciated.

Keith
------=_NextPart_000_00DE_01C6DCAD.1FAB49D0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 3521

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2963" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2>Hello all,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>I have a C# Windows Forms app.&nbsp; It is in
namespace App.GUI.&nbsp; It builds to Nav.exe.&nbsp; I have entered an
application level setting using the designer.&nbsp; Its type is string, name is
"FOO" and value is "monkey".&nbsp; I've tried the following ways to retrieve the
value and only one works:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>//Returns null</FONT></DIV>
<DIV><FONT face=Arial size=2>Configuration config =
ConfigurationManager.OpenExeConfiguration(Assembly .GetEntryAssembly().Location);</FONT></DIV>
<DIV><FONT face=Arial size=2>string monkey =
config.AppSettings.Settings["FOO"];</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>//Returns null - I know this is
deprecated</FONT></DIV>
<DIV><FONT face=Arial size=2>string monkey =
ConfigurationSettings.AppSettings["FOO"];</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>//Returns null</FONT></DIV>
<DIV><FONT face=Arial size=2>string monkey =
ConfigurationManager.AppSettings["FOO"];</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>//Returns correct value</FONT></DIV>
<DIV><FONT face=Arial size=2>string monkey =
global::App.GUI.Properties.Settings.Default["FOO"].ToString();</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>The Nav.exe.config file is in the right
place.&nbsp; The Configuration object (first example) contains the correct path
and its HasFile property is True.&nbsp; A Label bound to FOO displays the
correct value.&nbsp; Looking at the XML in Nav.exe.config shows</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial
size=2>&lt;applicationSettings&gt;<BR>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;App.GUI.Properties.Settings&gt;<BR>&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&lt;setting
name="FOO"
serializeAs="String"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&lt;value&gt;monkey&lt;/value&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/setting&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;
&lt;/App.GUI.Properties.Settings&gt;<BR>&nbsp;&nbsp;&nb sp;
&lt;/applicationSettings&gt;</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>So what am I doing wrong?&nbsp; Using global::
isn't very helpful when trying to get at these settings from another assembly
(BTW all the methods that fail in the .exe fail identically in satellite
assemblies)</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Any helpful advice or suggestions are greatly
appreciated.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Keith</FONT></DIV></BODY></HTML>

------=_NextPart_000_00DE_01C6DCAD.1FAB49D0--
Oct 11 '06 #5

P: n/a
I'm not sure what is malfunctioning here, Keith.

I can tell you that I have an ASP.Net 2.0 application, and settings
retrieval from the Web.Config works fine using the strongly typed access
provided by the Settings object:

string monkey = <namespace>.Settings.Default.FOO;

And contrary to other posts, I do not just get the default values; If I
switch to a non-default value in the Web.Config, it materializes at runtime.
I wonder if there could be a bug that is specific to the non-web apps that
use exename.exe.config.
"Keith" <ke***@alh.comwrote in message
news:uj**************@TK2MSFTNGP05.phx.gbl...
>I appreciate the responses, however there seems to be something essential
I'm missing. Why don't the methods for loading config information directly
from the app.settings file actually work? All the examples on the net
simply load the file and get the settings by name and off they go. This
does NOT work for me in any way.

I can get settings via Properties.Settings in the project for the main
assembly, but what about satellites? Unless I pass in the settings
collection (or something similar) that information isn't available to them
since loading from the app.config doesn't work. They don't even work for
for the main assembly!

Has anyone actually used the ConfigurationManager successfully? How about
retrieving app.config information from a satellite assembly? MVP's I'd
appreciate some clarification here.

Thanks!

"Chris Dunaway" <du******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
>Ciaran O''Donnell wrote:
>>You should just be able to write
Properties.Settings.Default["FOO"].ToString();

I believe that this always returns the *default* value and not
necessarily the value in the file. Look at the code behind file for
the settings. As you add settings, it inserts new properties to the
Settings class so you can declare an instance of this class and use it
for your settings. One benefit is that you don't specify your setting
using a string literal like "FOO" so you reduce the chance for a typo.

What we have done is something like this:

using App.UI.Properties;

Settings _settings = new Settings();
string value = _settings.FOO;

I'm not 100% sure this is necessary, but it works.

>I have a C# Windows Forms app. It is in namespace App.GUI. It builds
to Nav.exe. I have entered an application level setting using the
designer. Its type is string, name is "FOO" and value is "monkey".
I've tried the following ways to retrieve the value and only one
works:

//Returns null
Configuration config =
ConfigurationManager.OpenExeConfiguration(Assembl y.GetEntryAssembly().Location);
string monkey = config.AppSettings.Settings["FOO"];

//Returns null - I know this is deprecated
string monkey = ConfigurationSettings.AppSettings["FOO"];

//Returns null
string monkey = ConfigurationManager.AppSettings["FOO"];

//Returns correct value
string monkey =
global::App.GUI.Properties.Settings.Default["FOO"].ToString();

The Nav.exe.config file is in the right place. The Configuration
object (first example) contains the correct path and its HasFile
property is True. A Label bound to FOO displays the correct value.
Looking at the XML in Nav.exe.config shows

<applicationSettings>
<App.GUI.Properties.Settings>
<setting name="FOO" serializeAs="String">
<value>monkey</value>
</setting>
</App.GUI.Properties.Settings>
</applicationSettings>

So what am I doing wrong? Using global:: isn't very helpful when
trying to get at these settings from another assembly (BTW all the
methods that fail in the .exe fail identically in satellite
assemblies)

Any helpful advice or suggestions are greatly appreciated


Oct 25 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.