1

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.

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
  • similar post here: http://stackoverflow.com/questions/2260317/change-a-web-config-programmatically-with-c-net – dimko1 Oct 10 '11 at 12:11
  • It is a similar question, but deals only with maniulating the object in a file. Also, most of the answers (and information I've found elsewhere) deal with manipulating the web.config of the current site. What I want to do is to create and maniulate a web.config in a desktop app. – Paul Michaels Oct 11 '11 at 06:38

2 Answers2

0

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.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

cannot answer you via comments:)

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

dimko1
  • 872
  • 7
  • 15
  • Sorry, I may have mislead you. I am creating a web.config for a website. However, the program to do so is a desktop app. That is the purpose of the app - to create a web.config. – Paul Michaels Oct 11 '11 at 07:06
  • web.config file is a simple xml. So, i think it is easy to create simple application which will modify this xml. – dimko1 Oct 11 '11 at 09:03