Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1785
  • Last Modified:

App.Config Recompile

App.config in c# projects seems like a useful idea.  The usefulness is greatly dimished though if you have to recompile code for changes in App.Config to be reflected in the running code.  I assumed you wouldn't have to recompile and even found an article explaining that this was the case:

http://www.c-sharpcorner.com/Code/2002/Oct/DynamicProperties.asp

After some investifation though it seems that either I am doing something fundamentally wrong or you really do have to recompile.  I created a quick console application to test this:

using System;

namespace AppConfigTest
{
      class Class1
      {
            [STAThread]
            static void Main(string[] args)
            {
                  Dog objDog = new Dog();

                  while (true)
                  {
                        Console.ReadLine();
                        Console.Write(objDog.Poo());
                  }
            }
      }
}

and a Dog class:

using System;
using System.Configuration;

namespace AppConfigTest
{
      public class Dog
      {
            public Dog()
            {

            }

            public string Woof()
            {
                  return "Woof Woof";
            }

            public string Poo()
            {
                  return "Does a big " + ConfigurationSettings.AppSettings["PooColour"] + " poo";
            }
      }
}

And an App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
      <appSettings>
            <add key="PooColour" value="Yellow" />
      </appSettings>
</configuration>

Now I run the code, press a few keys, make changes to either the original App.config or the one placed in the bin folder but still my dog poo remains yellow.

Can anyone shed any light on this and bring multi coloured abilities to my dog poo?

Thanks

Dave
0
daveamour
Asked:
daveamour
1 Solution
 
smeggheadCommented:
The contents of the app.config file (or actually yourapp.exe.config) are read in at the point of running your app, not every time you try to access an element from it.

ps. like the class :-)
0
 
wile_e_coyoteCommented:
The App.Config file for a console app is only read once (when the app starts).   Any changes made to the App.Config will not be "seen" by the app until the next time it is launched.

You *could* write code to periodically (re)read the App.Config file (it is just an XML text file, after all), but I get the impression you're looking for something a little more elegant than that.

0
 
TheAvengerCommented:
The most important thing: if you want to change the settings, change the YourApplicatoin.Exe.config in the OUTPUT folder of your project (normally it's the Debug\Bin or Release\Bin), and not the app.config in the project folder. Then restart the application (not from the environment but directly the EXE file and yuppiii, it's ok
0
 
daveamourAuthor Commented:
Ahh ok this makes sense.  It would have been nice if MS included an option for how the App.config is read - once only or in real time.  My problem is that my code is a mission critical windows service which cannot easily be stopped and started.  Not to worry though, I can simply write my own config stuff although MS made ConfigurationSettings sealed :(

I was really just curious about how things worked rather than having a real problem.

Thanks guys

Dave
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now