Manipulating web.config in memory

662 views Asked by At

I have a web.config file open in a textbox and would like to manipulate it. For example, allowing the user to change the DBConn string or something. At the stage of trying to change this, the config text may, or may not exist as a physical file. Is it possible to use the WebConfigurationManager, or similar in memory; something like this:

string myWebConfig = LoadWebConfigHere();

var config = WebConfigurationManager.OpenWebConfiguration(myWebConfig);

OpenWebConfiguration only accepts a path. I don't really want to save it and reload it to get this functionality.

2

There are 2 answers

0
Grant Thomas On

This isn't possible, the web.config is only loaded once per request and changes cannot be "set"; any alterations to the file will only take effect once, say, the page is refreshed / reloaded.

So, whether this is an existing file, or is being compiled by means of your own logic, the above is the only way to utilise it by way of loading it into context.

2
dimko1 On

cannot answer you via comments:)

For desktop application you better to use external configuration file or app.config.