473,386 Members | 1,706 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,386 software developers and data experts.

How to write the data back to the config file... ?

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
Nov 21 '05 #1
4 1672
You can't. The best you can do is to use the System.Xml namespace to
manually open the file and write your data to it.

Keep in mind, however, that the app.config was not meant to store user
settings. Normally you would use a separate .config or .xml file to
store your user settings.

Do a Google search for the Microsoft Enterprise Library. It has an
entire application block devoted to storing your own configuration
settings.

Nov 21 '05 #2
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

Nov 21 '05 #3
And oh yea, sorry it's in C#.

Steve

"Steve Long" <St**********@NoSpam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #4
Thank you.
Jeff

"Steve Long" <St**********@NoSpam.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
And oh yea, sorry it's in C#.

Steve

"Steve Long" <St**********@NoSpam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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



Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
33
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually...
2
by: Shaun Ram | last post by:
Hi I have this constraint. A help would be greatly apprecitated. I have this Config file. <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="ITASCA">...
9
by: ALI-R | last post by:
Hi,, I have two questions : 1) Is it mandatory that config file of a desktop application must be App.config 2) Is it possible to update config file in your code?? thanks for your help. ALI
4
by: jasiglam | last post by:
Using the visual studio.net text editor, I know how to enter/change the "key/value" in the app.config file, for example: key="SqlConnection1.ConnectionString" value="data source=Boomer;initial...
3
by: Agnes | last post by:
Can I write a file like config.ini which store the server-name, userid & password, So, my application can read the config.ini file and get the server - name, Does my concept is correct ? If...
3
by: eieiohh | last post by:
MySQL 3.23.49 PHP 4.3.8 Apache 2.0.51 Hi All! Newbie.. I had a CRM Open Source application installed and running. Windows Xp crashed. I was able to copy the contents of the entire hard...
2
by: Alan T | last post by:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="DataBaseConnection" connectionString="server=localhost;database=SalesDB;User
4
by: apriebe47 | last post by:
Alright, I realize this is probably very basic to be posted on this newsgroup but I cannot figure out what is causing my problem. Here is the code I am using below: from getpass import getpass ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.