Dynamically Update Config File
There are scenarios where data need to be changed in the web.config at
Let's take an example, where session states need to be changed in web.config on the bases of options selected by user.
Isn't a good example..?
So the application changes the value of "mode" under SessionState node of web.config and uses the value as InProc or SQLServer, for maintaining the session state.
I know after reading the above dynamics change in web.config file, as per the requiremaent, you would like to see the actual code to make this possible.
Please see the code for changing the config file at the run-time..
void Application_Start(object sender, EventArgs e)
// Code that runs on application startup
config = WebConfigurationManager.OpenWebConfiguration("~");
SessionStateSection SessionState = config.GetSection("system.web/sessionState") as SessionStateSection;
if (SessionState != null)
SessionState.Mode = System.Web.SessionState.SessionStateMode.InProc;
//SessionState.Mode = (SessionStateSection)"Mode=InProc";
So, similary there can be different cases where we can dynamically change
the value of config file.