Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

Using ConfigurationManager/ConfigurationSettings to retrieve settings returns null values

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Keith - 25 Sep 2006 21:43 GMT
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
Keith - 28 Sep 2006 17:34 GMT
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

> Hello,
>
[quoted text clipped - 36 lines]
>
> Keith
GS - 02 Oct 2006 18:13 GMT
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

> Hello,
>
[quoted text clipped - 15 lines]
> ConfigurationManager.AppSettings[<settingname>]
> ConfigurationSettings.AppSettings[<settingname>]

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

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

> ExeConfigurationFileMap efm = new ExeConfigurationFileMap();
> efm.ConfigFileName  = Assembly.GetEntryAssembly().Location + ".config";
[quoted text clipped - 12 lines]
>
> Keith
Keith - 03 Oct 2006 00:33 GMT
> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.