By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 1,923 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

Re-cache the app.config file?

P: n/a
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.
Jul 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Dan Olson" <Da******@discussions.microsoft.com> wrote in message
news:C9**********************************@microsof t.com...
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.
Jul 21 '05 #2

P: n/a
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??

"JStewart" wrote:

"Dan Olson" <Da******@discussions.microsoft.com> wrote in message
news:C9**********************************@microsof t.com...
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.

Jul 21 '05 #3

P: n/a
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/247referenc...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" <Da******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
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??

"JStewart" wrote:

"Dan Olson" <Da******@discussions.microsoft.com> wrote in message
news:C9**********************************@microsof t.com...
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.

Jul 21 '05 #4

P: n/a
A possibility for you to consider....

http://www.kjmsolutions.com/appconfigstuff.ZIP

you will need to place the config file in the bin directory.
"Dan Olson" <Da******@discussions.microsoft.com> wrote in message
news:C9**********************************@microsof t.com:
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.


Jul 21 '05 #5

P: n/a

On 17 Jun 2004 19:49, Dan Olson wrote:
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
Jul 21 '05 #6

P: n/a
Well I tried....

:)

"Simon Smith" <gh*****@community.com> wrote in message
news:d0******************************@ghytred.com:
On 17 Jun 2004 19:49, Dan Olson wrote:
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


Jul 21 '05 #7

P: n/a

Sorry for the late response. I did not see your answer.

In the dll itself you can state the app.config file you want. I regre
not being more thourough on the sample. I thought everyone would kno
to go in and change the path to their own config file but that is wha
I get for "thinking".

But your answer is cool. I tried to put soemthing together I though
would help.


Dan Olson wrote:
*Scorpion:

Thanks for you response. your zip file contains a dll that hides th
"good stuff." Since you didn't provide the source for the class(es
in that assy, I am assuming that you used a custom configuratio
handler, or read/write XML directly.

Because this solution requires that the .config file be in ./bin, i
appears that this solution is actually a custom configuration fil
and clearly requires bypassing the .NET configuration API. Am
mistaken?

"scorpion53061" wrote:
A possibility for you to consider....

http://www.kjmsolutions.com/appconfigstuff.ZIP

you will need to place the config file in the bin directory.
"Dan Olson" <Da******@discussions.microsoft.com> wrote in message
news:C9**********************************@microsof t.com:

-
scorpion5306
-----------------------------------------------------------------------
Posted via http://www.mcse.m
-----------------------------------------------------------------------
View this thread: http://www.mcse.ms/message772722.htm

Jul 21 '05 #8

P: n/a
>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
Jul 21 '05 #9

P: n/a
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:m.********@inova.SPAMBEGONE.ch]
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
Jul 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.