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> </DIV>
<DIV><FONT face=Arial size=2>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:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </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> </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> </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> </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> </DIV>
<DIV><FONT face=Arial size=2>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</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial
size=2><applicationSettings><BR>
<App.GUI.Properties.Settings><BR>   ; & nbsp; <setting
name="FOO"
serializeAs="String"><BR> &nbs p;
<value>monkey</value><BR> & nbsp;
</setting><BR>   ;
</App.GUI.Properties.Settings><BR> &nb sp;
</applicationSettings></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>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)</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Any helpful advice or suggestions are greatly
appreciated.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Keith</FONT></DIV></BODY></HTML>
------=_NextPart_000_00DE_01C6DCAD.1FAB49D0--