473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Re-cache the app.config file?

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
9 6492

"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
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
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
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

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
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

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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
8
by: Lothar Scholz | last post by:
Because PHP5 does not include the mysql extension any more is there a chance that we will see more Providers offering webspace with Firebird or Postgres Databases ? What is your opinion ? I must...
1
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id ...
1
by: Brian | last post by:
I have an array like this: $events = array( array( '2003-07-01', 'Event Title 1', '1' //ID Number (not unique) ), array( '2003-07-02',
1
by: Clarice Almeida Hughes | last post by:
tenho um index onde tenho o link pro arq css, como sao visualizados pelo include todas as paginas aderem ao css linkado no index. so q eu preciso de alguns links com outras cores no css, o q devo...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.