That is correct and for good reason. When you make a change to the
machine.config/web.config files, it causes your application to restart. Not
too gracfully, I might add. There are a couple of work-arounds though.
A. Use a separate config file aside from web.config to hold runtime values
that might change.
B. Here is some code that will work, but USE IT AT YOUR OWN RISK. I have not
tested it, nor would I on a running asp.net application as it would cause
the application to restart as soon as it can.
using System.IO;
using System.Xml;
private void AddWebConfigVal ue(string name, string val)
{
try
{
//first we need to grab the web.config file.
string webconfig = Server.MapPath( "./web.config");
if(File.GetAttr ibutes(webconfi g) == FileAttributes. ReadOnly)
{
File.SetAttribu tes(webconfig, FileAttributes. Normal);
}
XmlDocument doc = new XmlDocument();
doc.load(webcon fig);
XmlNode node =
doc.DocumentEle ment.SelectSing leNode("configu ration/appSettings");
if(null != node)
{
XmlNode ne = doc.CreateNode( XmlNodeType.Ele ment,"add","");
ne.Attributes["key"] = name;
ne.Attributes["value"] = val;
node.AppendChil d(ne);
}
doc.Save(webcon fig);
File.SetAttribu tes(webconfig, FileAttributes. ReadOnly);
}
catch(Exception ex)
{
//aw crap... ;)
}
}
HTH,
Bill P.
"Blue Man" <bl**********@n ospam.com> wrote in message
news:e6******** ******@TK2MSFTN GP12.phx.gbl...
Anybody has idea how to save value in web.config file during runtime?
ConfigurationSe ttings.AppSetti ngs.Add("test", "test value");
won't work, it will cause and error : the file is read only during
rumtime.