Jeff,
I can share with you a class I wrote that uses a config file of your
specification. It expects the same format as the regular config file. You'll
have to write your own updating code though. On the plus side, if you change
something in it's config file from outside of the program, it'll detect it
and reload everything. It uses the normal Get and GetValues methods to
return values in the way the normal one does. This class is primed for
modification and extending though. It's just a bare beginning but works
pretty well so far. Check it out.
using System;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
using CCollections;
namespace CCArcSDEService
{
public delegate void ConfigChangedEventHandler(object sender, EventArgs e);
/// <summary>
/// Summary description for CAppInit.
/// Opens the XML Initializtion document specified
/// in the constructor. Use like Configuration.AppSettings.
/// </summary>
public class CAppInit
{
#region Private member objects
private FileSystemWatcher m_fsw;
private XmlDocument m_xdoc;
private XmlElement m_root;
private DateTime m_lastChanged;
private CICollection m_sections;
private NameValueCollection appSettings;
#endregion // private member objects
#region Private primitives
private string m_initFile;
#endregion // private primitives
public ConfigChangedEventHandler ConfigChanged;
public CAppInit(string InitFile)
{
try
{
m_initFile = InitFile;
if (!Init(InitFile))
throw new ArgumentException("Invalid xml configuration file");
int ipos = InitFile.LastIndexOf("\\");
string file = InitFile.Substring(ipos + 1);
string path = InitFile.Substring(0, ipos);
m_fsw = new FileSystemWatcher(path, file);
m_fsw.EnableRaisingEvents = true;
m_fsw.Changed += new FileSystemEventHandler(OnConfigChanged);
m_fsw.NotifyFilter = NotifyFilters.LastWrite;
}
catch(Exception e)
{
throw(e);
}
}
~CAppInit()
{
m_xdoc = null;
if (m_fsw != null)
m_fsw.Dispose();
m_fsw = null;
}
public virtual void OnConfigChanged(object sender, FileSystemEventArgs e)
{
if (ConfigChanged != null)
{
if (e.ChangeType == WatcherChangeTypes.Changed)
{
DateTime dte =File.GetLastWriteTime(m_initFile);
if (dte > m_lastChanged)
{
m_lastChanged = dte;
Reload();
ConfigChanged(this, e);
}
}
}
}
/// <summary>
/// Returns the comma delimited values for a key in a section
/// </summary>
/// <param name="section">the section from which to retrieve values, e.g.,
appSettings</param>
/// <param name="key">the key to look up the values</param>
/// <returns></returns>
public string Get(string section, string key)
{
CCNameValueCollection col = (CCNameValueCollection)m_sections[section];
if (col != null)
return col.Get(key);
else
return null;
}
public string[] GetValues(string section, string key)
{
CCNameValueCollection col = (CCNameValueCollection)m_sections[section];
if (col != null)
return col.GetValues(key);
else
return null;
}
public string[] GetKeys(string section)
{
CCNameValueCollection col = (CCNameValueCollection)m_sections[section];
if (col != null)
return col.GetKeys();
else
return null;
}
#region Private member methods
private void LoadSections()
{
if (m_sections != null)
{
m_sections.Clear();
m_sections = null;
}
CCNameValueCollection col;
m_sections = new CICollection(false);
XmlNodeList list = m_root.ChildNodes;
foreach (XmlNode node in list)
{
if (node.ChildNodes.Count > 0)
{
col = new CCNameValueCollection(node.ChildNodes.Count);
col.Key = node.Name;
foreach (XmlNode node1 in node.ChildNodes)
{
try
{
string key = node1.Attributes["key"].Value;
string val = node1.Attributes["value"].Value;
col.Add(node1.Attributes["key"].Value, node1.Attributes["value"].Value);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
m_sections.Add(col.Key, col);
}
}
}
private bool Reload()
{
m_xdoc = null;
return Init(m_initFile);
}
private bool Init(string InitFile)
{
try
{
m_xdoc = new XmlDocument();
m_xdoc.Load(InitFile);
m_root = m_xdoc.DocumentElement;
LoadSections();
}
catch
{
return false;
}
return true;
}
#endregion // Private member methods
}
}
Steve
"Jeff smith" <Je**********@hotmail.com> wrote in message
news:eZ****************@TK2MSFTNGP10.phx.gbl...
I am reading the value from config file using the below statement.
System.Configuration.ConfigurationSettings.AppSett ings("MY_DATA")
How to write data back to the same location?
I am using .Net Framework 1.1.
Thanks,
Jeff