Solved

App.Config Recompile

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now