Friday, November 2, 2007

Dynamically Writing into Web.config File

Inline
    XmlDocument xDoc = new XmlDocument(); // Gobal Declaration
string Path; // Gobal Declaration
int i; // Gobal Declaration
protected void Page_Load(object sender, EventArgs e)
{
i = Convert.ToInt32(ConfigurationManager.AppSettings["Test"].ToString());
}
protected void bt1_Click(object sender, EventArgs e)
{
Path = Server.MapPath("~/web.config");
xDoc.Load(Path);
XmlNodeList NodeList = xDoc.GetElementsByTagName("appSettings");
XmlNodeList tNodeList = NodeList[0].ChildNodes;
XmlAttributeCollection xAttColl = tNodeList[0].Attributes;
xAttColl[0].InnerXml = "Test";
xAttColl[1].InnerXml = Convert.ToString(i + 1);
xDoc.Save(Path);
Response.Write(ConfigurationManager.AppSettings["Test"].ToString());
}

Web.Config

<configuration>

<appSettings>

<add key="Test" value="0" /> <!-- Add This In Your WebConfig File -->

</appSettings>

</configuration>



No comments:

Creative Commons License
Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.