Solved

App.Config Recompile

Posted on 2003-12-02
4
1,749 Views
Last Modified: 2012-06-27
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
Comment
Question by:daveamour
4 Comments
 
LVL 10

Accepted Solution

by:
smegghead earned 25 total points
ID: 9860633
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
 
LVL 4

Expert Comment

by:wile_e_coyote
ID: 9860642
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
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9862451
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
 
LVL 19

Author Comment

by:daveamour
ID: 9865023
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question