Solved

App.Config Recompile

Posted on 2003-12-02
4
1,741 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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