writing to config file

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

Guest

Below is the code that i using to change the config file settings. When I do
the changes to the config file through this code, It seems that it updated
the config file with the changes, but when I got the file, the changes are
there.

I am having hard time finding out why this is happening.

Any help would be appreciated.

The call that I making to writeSettings is :

configSettings.WriteSetting("connectionstring", "Data Source=prsql;Persist
Security Info=True;User ID=apps;Password=pass;Unicode=True;Pooling=false;Min
Pool Size=4;Max Pool Size=100;Connection Lifetime=0;");



public static void WriteSetting(string key, string value)
{
// load config document for current assembly
XmlDocument doc = loadConfigDocument();

// retrieve appSettings node
XmlNode node = doc.SelectSingleNode("//appSettings");

if (node == null)
throw new InvalidOperationException("appSettings section not
found in config file.");

try
{
// select the 'add' element that contains the key
XmlElement elem =
(XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));

if (elem != null)
{
// add value for key
object ro = elem.GetAttribute("value");
elem.SetAttribute("value", value);
}
else
{
// key was not found so create the 'add' element
// and set it's key/value attributes
elem = doc.CreateElement("add");
elem.SetAttribute("key", key);
elem.SetAttribute("value", value);
node.AppendChild(elem);
}
doc.Save(getConfigFilePath());
}
catch
{
throw;
}
}

private static XmlDocument loadConfigDocument()
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(getConfigFilePath());
return doc;
}
catch (System.IO.FileNotFoundException e)
{
throw new Exception("No configuration file found.", e);
}
}

private static string getConfigFilePath()
{
return @"C:\app\debug\bin\app.exe.config";

}
 
Did you flush and close file after complete writings to config file before
reading it?
 
Did you flush and close file after complete writings to config file before
reading it?

Also, will this not cause the app to recycle...?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top