Write on Web.Config file from C#

chokka used Ask the Experts™
VS 2008 / C#.Net

In run time, i need to edit or rewrite my config file.

We have a feature, for Admin can change folder, file settings on the .aspx page. These informations are stored on config file instead of database. Infact, i need to rewrite some of the database connection strings also.

How to write / rewrite (edit) a config file on runtime ??
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011
This code will show how to change Connection String which is in Web.config file at Run Time.
Top Expert 2009

This is a major security hole. I would not recommend it. You should consider somewhere else to store these settings, as giving write access to web.config opens up your whole server to a possible hack.

Changes to it will also cause restart of the app domain.

You need to edit the user under which IIS runs your app domain and give that user write privileges to that directory or file. I recommend you instead move these changeable settings to a different file, even if you must reference that file from web.config.



mrjlotcola - Can you please explain about security hole ? And what other option you recommend us to proceed ??

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial