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.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)
Articles
- Articles(C#.NET) (3)
- ASP.NET (6)
- Asp.Net(Email) (1)
- Asp.net(Image) (2)
- Asp.Net(Web.Config) (2)
- C#.NET (5)
- C#.NET Threading (3)
- C#.NET(ASP.NET) (4)
- Comments in PHP (1)
- Encryption In PHP (1)
- iPhone (Articles) (1)
- JavaScript (1)
- Json with PHP (1)
- LINQ (1)
- PHP (2)
- PHP Constant (1)
- PHP Operators (1)
- PHP Print Command (1)
- PHP Tutorial (2)
- Strings In PHP (1)
- Variable Declaration In PHP (1)
- WPF (1)
- XAML (2)
About Me
Help Link
Followers
Posted by
Sreejith A.K
Wednesday, September 29, 2010
Labels:
Asp.Net(Web.Config)
Subscribe to:
Post Comments (Atom)