Software Programs and Projects

This Software Programs and Projects include programing tips and technology using different languages like VC++,ASP,JSP C#.Net,PHP,VB.Net,JavaScript,ASP.NET .Software Programs and Projects blog mainly support to software programmers and provide help through the mail.

How can update Web.config File in the Asp.NET ?

Update
AppSettings in the web.Config File (Asp.net)


public bool ChangeAppSettings(string strKey, string strValue)
{
XmlDocument doc = new XmlDocument();
bool bChange = false;
string configFile = Server.MapPath("web.config"); //Path.Combine(strSiteFolder, "web.config");
doc.Load(configFile);

XmlElement Root = doc.DocumentElement;

Dictionary appSettings = new Dictionary();
//appSettings.Add("AlertThreshold", "15");
appSettings.Add(strKey, strValue);
XmlNode appNode = Root["appSettings"];
foreach (XmlNode node in appNode.ChildNodes)
{
if (node.Attributes != null)
{
try
{
string key = node.Attributes.GetNamedItem("key").Value;
string value = node.Attributes.GetNamedItem("value").Value;
if (appSettings.ContainsKey(key) && value != appSettings[key].ToString())
{
node.Attributes.GetNamedItem("value").Value = appSettings[key].ToString();
bChange = true;
}
}
catch (Exception)
{
throw new Exception("While reading the web.config, this line had no key/value attributes modify: " + node.InnerText);
}
}
}

if (bChange) //Update web.config only if changes are made to web.config file
{
try
{
doc.Save(configFile);
return true;
}
catch (IOException objException)
{
throw new Exception(objException.Message.ToString());
}
}
else
{
return false;
}
}


protected void btnSetThreshold_Click(object sender, EventArgs e)
{
try
{
const string KEY_THRESHOLD = "AlertThreshold";
string strValue = this.txtWebThreshold.Text.Trim();
if (ChangeAppSettings(KEY_THRESHOLD, strValue))
{
this.lblStatus.Text = "Web Threshold Updated Succefully";
}
}
catch (Exception objException)
{
throw new Exception(objException.Message.ToString());
}
}
#endregion



tag:How can update Web.config File in the Asp.NET
Update Web.Config File
Update AppSettings in the web.Config File (Asp.net)

0 comments:

Post a Comment

Post a Comment