Using ConfigurationManager/ConfigurationSettings to retrieve settings returns null values

K

Keith

Hello,

Using VS2005, I have a Windows Forms app that uses the default
Settings.settings file. When the app is built, the properly-named
app.settings file is built into the output directory, and it contains the
correct application-level settings. Controls in the app that are bound to
settings in the app.config display the correct values. When values are
changed by hand in the app.settings file these new values are correctly
displayed when the app is run. In code, the following calls correctly
retrieve settings values:

global::<namespace>.Properties.Settings[<settingname>]
Properties.Settings[<settingname>]
Properties.Settings.<settingname>

However, the following functions always return null values:

ConfigurationManager.AppSettings[<settingname>]
ConfigurationSettings.AppSettings[<settingname>]

ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location).AppSettings.Settings[<settingname>]

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings[<settingname>]

ExeConfigurationFileMap efm = new ExeConfigurationFileMap();
efm.ConfigFileName = Assembly.GetEntryAssembly().Location + ".config";
ConfigurationManager.OpenMappedExeConfiguration(efm,
ConfigurationUserLevel.None).AppSettings.Settings[<settingname>]

All returned Configuration objects have the correct filepath and
HasFile==true.

I can use the Properties.Settings syntax when I'm in the right class, but I
need to access these settings from a satellite assembly and don't want to
pass around references if I don't have to.

Can anyone tell me what I'm doing wrong, or why these function calls can't
manage to find my settings?

Keith
 
K

Keith

So no one has any advice or suggestions for me? MVP's? If someone has a
small sample project that works, could you post it so I can see if my dev
machine is somehow screwed up?

This is a great .NET feature, and I'd like to get it working so I can use
it.

Keith
 
G

GS

eh, according to vb.net 2005 express help, ConfigurationManager.AppSettings
returns a collection and hence should be access the way collection are to
be accessed

the following will give you an idea in vb. as for other languages, I am sure
you can look it up under configurationmanager.appsetting

' Get appSettings.
Shared Sub GetAppSettings()
' Get the appSettings.
Dim appSettings As NameValueCollection = _
ConfigurationManager.AppSettings

' Get the collection enumerator.
Dim appSettingsEnum As IEnumerator = _
appSettings.Keys.GetEnumerator()

' Loop through the collection and
' display the appSettings key, value pairs.
Dim i As Integer = 0
While appSettingsEnum.MoveNext()
Dim key As String = appSettings.Keys(i)
Console.WriteLine("Name: {0} Value: {1}", _
key, appSettings(key))
i += 1
End While
End Sub 'GetAppSettings


Actually if you only have scalar simple application settings and no such
thing as user1, user2 or nor anything with multiple values,
My.Settings.<heightor whatever> is simpler



Keith said:
Hello,

Using VS2005, I have a Windows Forms app that uses the default
Settings.settings file. When the app is built, the properly-named
app.settings file is built into the output directory, and it contains the
correct application-level settings. Controls in the app that are bound to
settings in the app.config display the correct values. When values are
changed by hand in the app.settings file these new values are correctly
displayed when the app is run. In code, the following calls correctly
retrieve settings values:

global::<namespace>.Properties.Settings[<settingname>]
Properties.Settings[<settingname>]
Properties.Settings.<settingname>

However, the following functions always return null values:

ConfigurationManager.AppSettings[<settingname>]
ConfigurationSettings.AppSettings[<settingname>]
ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Locati
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSe
ttings.Settings[ said:
ExeConfigurationFileMap efm = new ExeConfigurationFileMap();
efm.ConfigFileName = Assembly.GetEntryAssembly().Location + ".config";
ConfigurationManager.OpenMappedExeConfiguration(efm,
ConfigurationUserLevel.None).AppSettings.Settings[<settingname>]

All returned Configuration objects have the correct filepath and
HasFile==true.

I can use the Properties.Settings syntax when I'm in the right class, but I
need to access these settings from a satellite assembly and don't want to
pass around references if I don't have to.

Can anyone tell me what I'm doing wrong, or why these function calls can't
manage to find my settings?

Keith
 
K

Keith

GS said:
eh, according to vb.net 2005 express help,
ConfigurationManager.AppSettings
returns a collection and hence should be access the way collection are to
be accessed

Yes I'm aware of that, but since the collection returned is derived from
NameValueCollection, you can index into the collection using either an int
or a string. Why iterate through a collection and check each key/value pair
when you can just ask explicitly for the item you want? Regardless,
iterating makes no difference; the settings are still not found.
Actually if you only have scalar simple application settings and no such
thing as user1, user2 or nor anything with multiple values,
My.Settings.<heightor whatever> is simpler

Simpler, yes, but only if you are in a Class or assembly that has access to
the original collection or its definition. This does not work if you are
trying to access settings from a satellite assembly that does not hold a
reference to the assembly that "owns" the settings classes. For example, it
you are using reflection to load .dlls.
 

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