PC Review


Reply
Thread Tools Rate Thread

config file changes

 
 
=?Utf-8?B?Vmlua2k=?=
Guest
Posts: n/a
 
      21st Nov 2007
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";

}
 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWljaGFlbCBOZW10c2V2?=
Guest
Posts: n/a
 
      21st Nov 2007
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";
>
> }

 
Reply With Quote
 
=?Utf-8?B?Vmlua2k=?=
Guest
Posts: n/a
 
      21st Nov 2007
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";
> >
> > }

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why this web config cannot reference an external config file? dh Microsoft C# .NET 0 2nd Oct 2009 12:12 AM
encrypting config file that aren't web config files... Ollie Riches Microsoft ASP .NET 1 4th Dec 2008 04:59 PM
Wiring up an App.Config file - deploying the App.Config file dbuchanan Microsoft Dot NET 12 27th Jul 2007 05:33 PM
sharedListeners config in app.config file doesn't recognize customlocation attribute RJ Microsoft Dot NET 0 1st Nov 2006 08:48 PM
using ReadXml to read exe.config file into Grid and update the exe.config using WriteXml hazz Microsoft C# .NET 0 7th Jul 2006 10:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:32 AM.