-----------------------------------------------------------
New Message on AhmedabadUserGroup
-----------------------------------------------------------
From: MVP_mayank
Message 1 in Discussion
Can I dynamically edit the web.config file in ASP.Net?<o:p></o:p>
If Yes How?
Regards
Abishek
Ans:
So here's my take on a convenient little class that allows you to either
modify, add or delete any appSettings element, in either your Executable,
Console or ASP.NET web application at runtime, on the fly. Bear in mind of
course, that if you modify a web.config on a running ASP.NET app, the ASP.NET
worker process will recycle. Users currently using your app aren't exactly
guaranteed to have a fun experience when this happens...
using System;
using System.Xml;
using System.Configuration;
using System.Collections;
using System.Reflection;
using System.Diagnostics ;
public enum ConfigFileType
{
WebConfig ,
AppConfig
}
public class AppConfig : System.Configuration.AppSettingsReader
{
public string docName = String.Empty;
private XmlNode node=null;
private int _configType;
public int ConfigType
{
get
{
return _configType;
}
set
{
_configType=value;
}
}
public bool SetValue(string key, string value)
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if( node == null )
{
throw new System.InvalidOperationException( "appSettings section not
found");
}
try
{
// XPath select setting "add" element that contains this key
XmlElement addElem= (XmlElement)node.SelectSingleNode("//[EMAIL PROTECTED]'"
+key +"']") ;
if (addElem!=null)
{
addElem.SetAttribute("value",value);
}
// not found, so we need to add the element, key and value
else
{
XmlElement entry = cfgDoc.CreateElement("add");
entry.SetAttribute("key",key);
entry.SetAttribute("value",value);
node.AppendChild(entry);
}
//save it
saveConfigDoc(cfgDoc,docName);
return true;
}
catch
{
return false;
}
}
private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath)
{
try
{
XmlTextWriter writer = new XmlTextWriter( cfgDocPath , null );
writer.Formatting = Formatting.Indented;
cfgDoc.WriteTo( writer );
writer.Flush();
writer.Close();
return;
}
catch
{
throw;
}
}
public bool removeElement ( string elementKey)
{
try
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if( node == null )
{
throw new System.InvalidOperationException( "appSettings section not
found");
}
// XPath select setting "add" element that contains this key to remove
node.RemoveChild( node.SelectSingleNode("//[EMAIL PROTECTED]'" +elementKey
+"']") );
saveConfigDoc(cfgDoc,docName);
return true;
}
catch
{
return false;
}
}
private XmlDocument loadConfigDoc( XmlDocument cfgDoc )
{
// load the config file
if( Convert.ToInt32(ConfigType)==Convert.ToInt32(ConfigFileType.AppConfig))
{
docName= ((Assembly.GetEntryAssembly()).GetName()).Name;
docName += ".exe.config";
}
else
{
docName=System.Web.HttpContext.Current.Server.MapPath("web.config");
}
cfgDoc.Load( docName );
return cfgDoc;
}
}
I'm sure the above can be improved, but it works just fine for me. Notice that
if you attempt to modify an element that doesn't exist, we assume that we need
to create it for you. In the downloadable solution below you'll find sample
projects for a Web application and a Winforms executable, both of which contain
sample code to use this class. Note that there are three subfolders under the
solution when you unzip this, and the one named "appConfigWeb" needs to be made
an IIS virtual directory / application. Enjoy!
Regards, <o:p></o:p>
Mayank Pujara ([EMAIL PROTECTED])<o:p></o:p>
Send Your Comments Here
Find just what you're after with the new, more precise MSN Search - try it
now!
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/AhmedabadUserGroup/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you
received this message by mistake, please click the "Remove" link below. On the
pre-addressed e-mail message that opens, simply click "Send". Your e-mail
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]