Hi Michael,
No, I am not doing that. I thought since it is an xml file. I don't have to
do that. I am just changing the node of the xml file.
I will try to do that.
Thanks.
"Michael Nemtsev" wrote:
> Did you flush and close file after complete writings to config file before
> reading it?
>
> --
> WBR, Michael Nemtsev [.NET/C# MVP].
> Blog: http://spaces.live.com/laflour
>
>
>
> "Vinki" wrote:
>
> > 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";
> >
> > }