Accessing the ApplicationSettings section of the App.config with C#

OK, this sounds like a no brainer, but I ran into an issue the other day and it took me forever to find the information needed.  I used the GUI in VS 2008 to create some strongly typed application settings.  This is easy to do.  Right click on the project and select Properties.

AppSet1

I created two settings: one is a DateTime and the other is a bool.  This is the easy part.  The hard part is getting and saving these settings in code.  Well, it’s not hard once you know what you are looking for.  I searched far and wide using ‘Application Settings’ and C#.  I found all kinds of document ion about the Mysettings namespace in VB.Net but nothing on C#.  I even scoured the documentation from MSDN.  I finally found an answer on a forum somewhere.  You use the ‘Properties.Settings.Default’ namespace.  So, I can access my settings using this code.

AppSet2

If nothing else just remember this post for future reference in case you run across this yourself.  For some reason the ‘Properties’ namespace just isn’t intuitive to me.  There is also a lot of confusion out there revolving around the difference between ‘Properties.Settings.Default’ and the ‘System.Configuration.AppSettingsSection’ namespaces. One of the reason I’m blogging on it is so I can reference it the next time I run into this.

Posted in |

18 comments:

  1. Anonymous Says:

    I get the error "Property or indexer 'blah' cannot be assigned to -- it is read only.

    Any ideas...

  2. Anonymous Says:

    Fixed my problem. This will not work if you set the settings scope to "application". You must set the setting to "user" to be able to write to it.

  3. Ben H Says:

    Thanks for pointing this out Bobby. You are right on this. The "application" settings cannot be changed at runtime. This seems kind of wierd to me, but I guess MS wants to make it clear the application level settings are for the application scope only.

  4. Anonymous Says:

    Thanks a ton for this. Was pulling my hair out trying to figure this out.

  5. Unknown Says:

    Application settings can be changed on run time, you need to "load" the config file, get the section, modified the value of the element and then save the config file.

  6. Ben H Says:

    Thanks for the feedback Ulises! Do have a URL to some code that demonstrates saving Application settings at run time? According to the documentation on MSDN http://msdn.microsoft.com/en-us/library/aa730869(vs.80).aspx application settings are read-only at runtime. I'd love to see a work around if you know of one.

  7. graemeklass Says:

    when I try and read the setting I get a compile error: "cannot be used in this context because it lacks the get accessor"
    Any ideas?

  8. Ben H Says:

    Hey graemeklass, This is an error that is usually related to properies that don't have the "Get" accessor defined. Meaning they are either not available or can only be assigned to and not read. Did you see Bobby's post above related to the Scope on the setting? Make sure the Scope is set to "user". Let me know if you get it figured out.

  9. Unknown Says:

    Ben,

    I have an example here:

    http://ulyontheweb.blogspot.com/2008/08/modify-application-settings-on-fly.html

    let me know if this helps

  10. Ben H Says:

    Hey Ulises,

    Thanks for the example! In your code you are getting the setting from "userSettings/GridSettings". This will work because you are in the User Settings sections. Have you ever changed things in the Application Settings section? I have to admit I have not tried getting the application settings using the ConfigurationManager class. It would seem with this class you could change any section. Let me know if you've been able to change settings that are scoped as applicatioin settings.

  11. Unknown Says: This comment has been removed by the author.
  12. Unknown Says:

    Thanks a lot! You saved my day.

  13. Anonymous Says:

    Good Article
    Ron

  14. Anonymous Says:

    молодежь порно фото http://free-3x.com/ порно пьяных малолеток free-3x.com/ посмотреть порно видео с малолетками [url=http://free-3x.com/]free-3x.com[/url]

  15. Anonymous Says:

    ...please where can I buy a unicorn?

  16. Anonymous Says:

    That’s Too nice, when it comes in india hope it can make a Rocking place for youngster.. hope that come true.

  17. Anonymous Says:

    Интересно написано....но многое остается непонятнымb

  18. Anonymous Says:

    I recently switched computer and had to authorize another computer for my itunes/iphone account. I noticed that my Iphone apps that I downloaded on the other computer were not present here, how do I transfer it into my now computer? Do I have to rebuy the apps?
    [url=http://unlockiphone22.com]unlock iphone[/url] [url=http://seniorfitnesscafe.com/forums/viewtopic.php?f=2&t=452&p=3405#p3405]unlock iphone[/url]