Re-cache the app.config file?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My question is not about how to update an application's config file during run-time, but how to invalidate the cached copy so that if a change to the config file is made, the application can use the new data without being restarted.

Is there a programmatic way to invalidate the cached copy of an app.config file?

In the "old" .INI file days, there was a work-around to this caching. How do we do it today?

Help is appreciated.
 
Dan Olson said:
My question is not about how to update an application's config file during
run-time, but how to invalidate the cached copy so that if a change to the
config file is made, the application can use the new data without being
restarted.
Is there a programmatic way to invalidate the cached copy of an app.config file?

In the "old" .INI file days, there was a work-around to this caching. How do we do it today?

Help is appreciated.

I'm not sure you can get the data to refresh if the file is modified, but
you could create a custom XML reader for your config file that at certain
events (eg from a FileSystemWarcher) reloads it. The normal AppSettings
collection is read only and there is no reload functionality.
 
That's my fear. I really don't want to have to build that whole infrastructure.

I was hoping someone could tell me how to invalidate the cached copy. That way I could use a FileSystemWatcher to trigger it, and my changes would be used by the program, but the rest of the time the benefits of caching would be there.

Thanks.

Anyone else?? Microsoft??
 
Hi Dan

I want to do the exact same thing as you. It's a minor issue that I'm not
willing to spend much time on at the moment, but when I searched I came
across the following post, it might help you:

http://www.dotnet247.com/247reference/msgs/34/174341.aspx

Please let me know if you get anywhere with this as I'm interested in
finding out how to do this without writing a custom app config reader

Kind Regards,
Steve

Dan Olson said:
That's my fear. I really don't want to have to build that whole infrastructure.

I was hoping someone could tell me how to invalidate the cached copy. That
way I could use a FileSystemWatcher to trigger it, and my changes would be
used by the program, but the rest of the time the benefits of caching would
be there.
 
My question is not about how to update an application's config file during
run-time, but how to invalidate the cached copy so that if a change to the
config file is made, the application can use the new data without being restarted.


Is there a programmatic way to invalidate the cached copy of an app.config
file?

In the "old" .INI file days, there was a work-around to this caching. How
do we do it today?

Help is appreciated.

It's not a huge deal. This implementation is cut down from a live class:

using System;
using System.Configuration;
using System.Xml;
using System.IO;
using System.Reflection;

namespace Xyz {

/// <summary>
/// Checks whether the private config specified needs to be reloaded
and does so /// </summary>
public class PrivateConfig {
private PrivateConfig() {
}
private static XmlDocument configDoc;
private static DateTime dateTimeOfLoadedFile;
private static XmlNode translations;
private static object lockWithThis = new object();


public static bool IsStale {
get {
bool isStale = false;
if (configDoc == null) {
isStale = true;
} else {
// Check file load time
string configPath = ConfigurationSettings.AppSettings["ConfigurationFile"];
FileInfo fi = new FileInfo(configPath);
if (fi.LastWriteTime > dateTimeOfLoadedFile) {
isStale = true;
}
}
return isStale;
}
}
public static XmlDocument Load() {
if (PrivateConfig.IsStale) {
string configPath = ConfigurationSettings.AppSettings["ConfigurationFile"];
lock(lockWithThis) {
FileInfo fi = new FileInfo(configPath);
if (fi.LastWriteTime > dateTimeOfLoadedFile) {
configDoc = new XmlDocument();
configDoc.Load(fi.FullName);
translations = null;
dateTimeOfLoadedFile = fi.LastWriteTime;
}
}
}
return configDoc;
}

public static string Value(string key) {
PrivateConfig.Load();
XmlNode node = configDoc.SelectSingleNode("descendant::" + key);
string retVal = "";
if (node != null) {
retVal = node.InnerText;
}
return retVal;
}
public static XmlNode Contents(string pathName) {
PrivateConfig.Load();
XmlNode contents = configDoc.SelectSingleNode("descendant::" + pathName);
return contents;
}
public static bool IsSet(string setting) {
PrivateConfig.Load();
XmlNode node = configDoc.SelectSingleNode("descendant::" + setting);
bool retVal = false;
if (node != null) {
string text = node.InnerText.ToLower();
if (text == "true" || text == "1") {
retVal = true;
}
}
return retVal;
}
}
}


Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook
 
Well I tried....

:)

Simon Smith said:
My question is not about how to update an application's config file
during
run-time, but how to invalidate the cached copy so that if a change to
the
config file is made, the application can use the new data without being
restarted.


Is there a programmatic way to invalidate the cached copy of an
app.config
file?

In the "old" .INI file days, there was a work-around to this caching. How

do we do it today?

Help is appreciated.

It's not a huge deal. This implementation is cut down from a live class:

using System;
using System.Configuration;
using System.Xml;
using System.IO;
using System.Reflection;

namespace Xyz {

/// <summary>
/// Checks whether the private config specified needs to be reloaded
and does so /// </summary>
public class PrivateConfig {
private PrivateConfig() {
}
private static XmlDocument configDoc;
private static DateTime dateTimeOfLoadedFile;
private static XmlNode translations;
private static object lockWithThis = new object();


public static bool IsStale {
get {
bool isStale = false;
if (configDoc == null) {
isStale = true;
} else {
// Check file load time
string configPath =
ConfigurationSettings.AppSettings["ConfigurationFile"];
FileInfo fi = new FileInfo(configPath);
if (fi.LastWriteTime > dateTimeOfLoadedFile) {
isStale = true;
}
}
return isStale;
}
}
public static XmlDocument Load() {
if (PrivateConfig.IsStale) {
string configPath =
ConfigurationSettings.AppSettings["ConfigurationFile"];
lock(lockWithThis) {
FileInfo fi = new FileInfo(configPath);
if (fi.LastWriteTime > dateTimeOfLoadedFile) {
configDoc = new XmlDocument();
configDoc.Load(fi.FullName);
translations = null;
dateTimeOfLoadedFile = fi.LastWriteTime;
}
}
}
return configDoc;
}

public static string Value(string key) {
PrivateConfig.Load();
XmlNode node = configDoc.SelectSingleNode("descendant::" + key);
string retVal = "";
if (node != null) {
retVal = node.InnerText;
}
return retVal;
}
public static XmlNode Contents(string pathName) {
PrivateConfig.Load();
XmlNode contents = configDoc.SelectSingleNode("descendant::" +
pathName);
return contents;
}
public static bool IsSet(string setting) {
PrivateConfig.Load();
XmlNode node = configDoc.SelectSingleNode("descendant::" + setting);
bool retVal = false;
if (node != null) {
string text = node.InnerText.ToLower();
if (text == "true" || text == "1") {
retVal = true;
}
}
return retVal;
}
}
}


Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook
 
That's my fear. I really don't want to have to build that whole infrastructure.

Hi Dan,

Well I have - re-written the whole stuff - to handle a number of these
things:

* Being able to specify *ANY* name for the config file (e.g. I can
also have a .config file for a DLL assembly - not just EXE)

* Load and re-load .config file at any time

* Save back .config file (also a Save As function)

I've written the code, and it's ready, and I was thinking of
publishing it on CodeProject. It's in Delphi for .NET right now, but
I'm about to rewrite it in C# (just as an exercise).

So if you're willing, you could beta-test it and let me know if it
works for you.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Just my two cents:

Consider using System.IO.IsolatedStoreage
http://www.kjmsolutions.com/appconfigsystemio.ZIP . It will appear your
app.config document does not change but your application remember your
settings.

http://www.kjmsolutions.com/xmlsettings.htm - good old fashioned xml -
my prefrerred approach

http://www.kjmsolutions.com/appconfigstuff.ZIP - put the current config
file in the bin directory or create your own and change the path in the
dll. This is not my best work and I have not had time to perfect it so
be kind :-)

________________________________

From: Marc Scheuner [MVP ADSI] [mailto:[email protected]]
Sent: Wednesday, July 07, 2004 1:13 AM
To: microsoft.public.dotnet.framework
Cc: microsoft.public.dotnet.general
Subject: Re: Re-cache the app.config file?


That's my fear. I really don't want to have to build that whole infrastructure.


Hi Dan,

Well I have - re-written the whole stuff - to handle a number of these
things:

* Being able to specify *ANY* name for the config file (e.g. I can
also have a .config file for a DLL assembly - not just EXE)

* Load and re-load .config file at any time

* Save back .config file (also a Save As function)

I've written the code, and it's ready, and I was thinking of
publishing it on CodeProject. It's in Delphi for .NET right now, but
I'm about to rewrite it in C# (just as an exercise).

So if you're willing, you could beta-test it and let me know if it
works for you.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Back
Top