+ Post New Thread
Results 1 to 14 of 14
Coding Thread, C# - App Settings in Coding and Web Development; Ok so following on from this thread ( http://www.edugeek.net/forums/coding...lay-timer.html ) How do I get C# to read settings from a ...
  1. #1

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,678
    Thank Post
    861
    Thanked 650 Times in 431 Posts
    Rep Power
    499

    C# - App Settings

    Ok so following on from this thread (C# - Button delay timer)

    How do I get C# to read settings from a configuration file (app.config?) that can be changed by the end user (ie the admin)

  2. #2

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,678
    Thank Post
    861
    Thanked 650 Times in 431 Posts
    Rep Power
    499
    ok half way there - I now have an app.config file that holds the location of the file

    Unfortunately any changes to this file by an end user doesn't have any effect

  3. #3

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    Use the settings classes - look under Settings in your project properties, and it will generate a settings file and classes for you.

  4. #4

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,678
    Thank Post
    861
    Thanked 650 Times in 431 Posts
    Rep Power
    499
    Tried that - but same problem - in fact there was no sign of a settigns file that could be editted

  5. #5

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    It should appear next to the .exe when you build. Make sure they are application settings and not user settings.

  6. #6

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,678
    Thank Post
    861
    Thanked 650 Times in 431 Posts
    Rep Power
    499
    nope - the app.config does but not via properties -> settings (aka Settings.settings in VS)

    But any changes to app.config doesnt take effect until i rebuild the solution.

  7. #7

    Join Date
    Feb 2010
    Posts
    1
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    You need to go to your web.config file... if you haven't got one add a Web.config file.

    Make sure you've got an <appSettings> section like this:-

    <configuration>
    <appSettings>
    <add key="settingName" value="valueYouWantToRead" />
    </appSettings>
    </configuration>

    Then add a reference to System.Net.Configuration

    and in code use:-

    string setting = System.Net.Configuration.ConfigurationManager.AppS ettings["settingName"]

    or something like that. Good luck.

  8. #8

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,678
    Thank Post
    861
    Thanked 650 Times in 431 Posts
    Rep Power
    499
    Ok got the Propect Properties -> Settings working and updating app.config

    But still cant change the value after its built - even though the scope is for a user...

    So, somehow the application isn't re-reading the app.config when its run..

  9. #9

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,877
    Thank Post
    518
    Thanked 2,486 Times in 1,928 Posts
    Blog Entries
    24
    Rep Power
    838
    Web.settings is only for web settings...

    App.config is a file that when you're building, you shouldn't edit directly from what I've seen - Visual Studio doesn't like it.

    The way I do it is - do all my original settings setting via the settings window in visual studio, reference them via Properties.Settings.Default.blah (remembering to save them if changing them during runtime via the program itself), and then editing the blah.exe.config file by hand if needs be, outside runtime.

    The app.config gets turned in to blah.exe.config when you rebuild - so any changes you make to app.config will only get transferred to the blah.exe.config when you rebuild.

  10. Thanks to localzuk from:

    Gatt (5th February 2010)

  11. #10

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,678
    Thank Post
    861
    Thanked 650 Times in 431 Posts
    Rep Power
    499
    Quote Originally Posted by Andy-3-16 View Post
    You need to go to your web.config file... if you haven't got one add a Web.config file.

    Make sure you've got an <appSettings> section like this:-

    <configuration>
    <appSettings>
    <add key="settingName" value="valueYouWantToRead" />
    </appSettings>
    </configuration>

    Then add a reference to System.Net.Configuration

    and in code use:-

    string setting = System.Net.Configuration.ConfigurationManager.AppS ettings["settingName"]

    or something like that. Good luck.
    Thats pretty much what I have now - except its app.config - since this isnt a web application I'm making..

    When I build the application it reads the value from app.config as it should, but doesnt change if app.config is edited post build

  12. #11

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,678
    Thank Post
    861
    Thanked 650 Times in 431 Posts
    Rep Power
    499
    Quote Originally Posted by localzuk View Post
    Web.settings is only for web settings...

    App.config is a file that when you're building, you shouldn't edit directly from what I've seen - Visual Studio doesn't like it.

    The way I do it is - do all my original settings setting via the settings window in visual studio, reference them via Properties.Settings.Default.blah (remembering to save them if changing them during runtime via the program itself), and then editing the blah.exe.config file by hand if needs be, outside runtime.

    The app.config gets turned in to blah.exe.config when you rebuild - so any changes you make to app.config will only get transferred to the blah.exe.config when you rebuild.
    OK that makes abit of sense - but how do i save them?

  13. #12

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,678
    Thank Post
    861
    Thanked 650 Times in 431 Posts
    Rep Power
    499
    yay! editting blah.exe.config worked!!

  14. #13

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,877
    Thank Post
    518
    Thanked 2,486 Times in 1,928 Posts
    Blog Entries
    24
    Rep Power
    838
    Quote Originally Posted by Gatt View Post
    OK that makes abit of sense - but how do i save them?
    Properties.Settings.Default.Save();

  15. Thanks to localzuk from:

    Gatt (5th February 2010)

  16. #14

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,678
    Thank Post
    861
    Thanked 650 Times in 431 Posts
    Rep Power
    499
    Cheers - will look at getting that added in

SHARE:
+ Post New Thread

Similar Threads

  1. App-V
    By DrCheese in forum How do you do....it?
    Replies: 22
    Last Post: 17th July 2009, 09:52 AM
  2. App-V 4.5
    By gshaw in forum Windows
    Replies: 9
    Last Post: 3rd February 2009, 01:28 PM
  3. App to Web App
    By Quackers in forum Web Development
    Replies: 8
    Last Post: 28th February 2007, 01:32 PM
  4. email app
    By russdev in forum ICT KS3 SATS Tests
    Replies: 0
    Last Post: 3rd July 2006, 04:46 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •