Configuration File

  • Thread starter Thread starter poifull
  • Start date Start date
P

poifull

If I made changes to the configuration file when the executable is running,
it seems like the changes are not detected. I've found the same problem
with ConfigurationSettings.GetConfig and ConfigurationSettings.AppSettings.
Is there any way aound it?

Thanks,
poifull
 
Hi,

the config file is only read once at the programm
start. You won't be able to get the changes with
the ConfigurationSettings class. Write your own
class that read direct from the config if called.

Cheers
Lars Behrmann

_________________
Nothing is impossible. UML is the key for all your problems.
AODL - Make your .net apps OpenOffice ready
http://aodl.sourceforge.net/
 
In addition to what Lars is saying, if you're making these changes
programmatically, you might want to cache them in your application, so you
always have the latest values. I realize you might be making them manually,
in which case you might want to read from a "dynamic" config file. Name this
config file something different than the App.config (which get's renamed
anyway), and read from it using your own class. You might want to use the
FileSystemWatcher class to be notified when the config file changes, and
then read in the new values.
 
Lars said:
Hi,

the config file is only read once at the programm
start. You won't be able to get the changes with
the ConfigurationSettings class. Write your own
class that read direct from the config if called.

Well, strictly speaking, it is read when the application domain starts.
This means that you could get round this by creating a new app domain
and reading the values there, but that is far from being a good
solution. Carsten's solution is the best answer, but bear in mind that
it will only work with your objects that use configuration. System
objects that get values from the configuration file (for example, the
tracing mechanism in System.Diagnostics) won't be affected.

Richard
 
Hello poifull !

..config files are "read-once-cache-forever" story i.e. app.config file
will be read as your application starts and even if you write anything
to it change wont get reflected untill you restart your application.

Let suppose you have some custom configuration element (as shown below)
in the .config file and you want to manipulate them at runtime i.e.
read and then write to it.

<DatabaseRepository>
<DataSources>
<DataSource name="sqlserver" connection-string="sql-conn-str" />
<DataSource name="oracle" connection-string="oracle-conn-str" />
</DataSources>
</DatabaseRepository>

then you can modify the scheme as:

<DatabaseRepository file-ref="dbrep.xml" />

where your "dbrep.xml" file will be a seperate file linked with your
app.config and your dbrep.xml file contains all the data.

// file name
String fileName = "<some_xpath_expression_to_get_the_attribute>";
// Load the config file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);

This way you can read your data from the in-memory DOM and can even
write to the in-memory DOM and later when you are finish with your work
simply deserialize it to have a new updated config file.

I hope this might be of some help.

Let me know in case of any inconsistancy.

Regards,

Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd
 
Back
Top