Click here to Skip to main content
11,932,461 members (47,982 online)
Click here to Skip to main content
Add your own
alternative version


77 bookmarked

Modifying Web.Config During Installation

, 26 Jan 2006
Rate this:
Please Sign up or sign in to vote.
How to modify the Web.Config of a WebService during installation.


This application illustrates an installer that changes the values in a webservice's web.config file during setup, according to user entry. The application changes values of key values in the web.config file.


As web.config is a configuration file for ASP.NET applications or ASP.NET web service applications to store data, it can be modified by the user after application setup using Notepad or programmatically. Most programmers use web.config as it stored data that can be changed dynamically by the user.

  <add key="Name"  value="Keyvalue"/>

A user might have to open and change this file many times if a web application or service is moved from one server to another frequently, so in the installer I've enabled a facility for a user to change the values of attributes of his choice.

My example in this application changes the connection string used by a web service.

  <add key="Main.ConnectionString" 
               User ID=sa;Password=;

So in the installer, I change the key value by implementing the installer class in a webservice project.

Then in the installer class, I override the Install function:

public override void Install(IDictionary stateSaver)
    //You type here your own code
    base.Install (stateSaver);

To get data required to change the values in web.config from the user, I add in the setup project, a custom action:

I the change custom data property for it to:


[EDITA1], [EDITA2], [EDITA3], [EDITA4] are names of textboxes in the dialog added in the setup project interface.

You will notice the value here:


I've separated the parameters to the Installer class by a special letter (@) to split them later as Visual Studio concatenates all the parameters to the first parameter and empty others.

void FormateParamters()
    string strServer,strUser , strPassword ,strFolder;
    EventLog.WriteEntry("TNT", Context.Parameters["Server"]);






The previous function is to format the parameters by splitting them using the special character (@) so we can use them later for the connection string reconstruction function:

void CreateConnectionString()

    Assembly ass=Assembly.GetExecutingAssembly();
    EventLog.WriteEntry ("Web.Config", 

    Stream stmConfig=ass.GetManifestResourceStream(


    FileStream stmPhysical=new FileStream(
    StreamReader srConfig=new StreamReader(stmConfig);
    StreamWriter swConfig=new StreamWriter(stmPhysical);

    string strConfig=srConfig.ReadToEnd();
    strConfig=strConfig.Replace("server=(local);database" + 
              "=DatabaseName;User ID=sa;Password=;" + 

    swConfig.Close ();


The above function copies the web.config from the physical path and begins to replace the old key value with the new value.

Note: the local folder parameter must be the same path where the webservice is installed in. In this case, it is:



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Amal ElHosseiny
Web Developer
Egypt Egypt
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionWeb.config edit Pin
kaushikpathak18-Oct-13 1:14
memberkaushikpathak18-Oct-13 1:14 
Questionhow to show the existing values in the textboxes before installation? Pin
Praveen Jeganathan22-Apr-13 1:58
memberPraveen Jeganathan22-Apr-13 1:58 
GeneralMy vote of 4 Pin
KiweenJain11-Nov-12 22:32
memberKiweenJain11-Nov-12 22:32 
GeneralCreate Web Service Installer Pin
Member 38889644-May-09 1:58
memberMember 38889644-May-09 1:58 
GeneralGreat article! Pin
Thomas ST21-Apr-09 4:02
memberThomas ST21-Apr-09 4:02 
GeneralPossible better way to acquire web.config path Pin
BillyGoatGruff8-Apr-09 6:22
memberBillyGoatGruff8-Apr-09 6:22 
GeneralRe: Possible better way to acquire web.config path [modified] Pin
Thomas ST21-Apr-09 3:57
memberThomas ST21-Apr-09 3:57 
QuestionModifing the Project for the Installer??? Pin
Ricardo Casquete3-Feb-08 17:16
memberRicardo Casquete3-Feb-08 17:16 
Generalproblem in installation Pin
chinni10000719-Sep-07 19:56
memberchinni10000719-Sep-07 19:56 
GeneralRe: problem in installation Pin
anand.lv16-Jan-09 3:26
memberanand.lv16-Jan-09 3:26 
GeneralRe: Problems when comments in web.config &lt;!-- Some Comment --> Pin
Amal ElHosseiny30-Mar-07 0:18
memberAmal ElHosseiny30-Mar-07 0:18 
GeneralRe: Problems when comments in web.config &amp;lt;!-- Some Comment --&gt; Pin
assadhf30-Mar-07 2:11
memberassadhf30-Mar-07 2:11 
GeneralSuggestion Pin
syemhusa22-Nov-06 0:42
membersyemhusa22-Nov-06 0:42 
GeneralLocal folder-path Pin
edgecrush3r27-Sep-06 1:46
memberedgecrush3r27-Sep-06 1:46 
Generalsorry ... broken download Pin
marquito_cuba27-Jul-06 7:18
membermarquito_cuba27-Jul-06 7:18 
GeneralRe: sorry ... broken download Pin
Amal ElHosseiny30-Jul-06 21:59
memberAmal ElHosseiny30-Jul-06 21:59 
GeneralUse of EventLog.WriteEntry In this article Pin
Auldrin John Possa22-Feb-06 17:23
memberAuldrin John Possa22-Feb-06 17:23 
GeneralRe: Use of EventLog.WriteEntry In this article Pin
Amal ElHosseiny3-Mar-06 23:26
memberAmal ElHosseiny3-Mar-06 23:26 
GeneralVery useful Pin
mosessaur13-Feb-06 4:05
membermosessaur13-Feb-06 4:05 
GeneralWeb Application Folder Pin
ash76-Feb-06 0:57
memberash76-Feb-06 0:57 
GeneralNice Job Pin
Brent Finney2-Feb-06 9:16
memberBrent Finney2-Feb-06 9:16 
GeneralProblem Pin
ash72-Feb-06 1:51
memberash72-Feb-06 1:51 
GeneralRe: Problem Pin
Amal ElHosseiny2-Feb-06 1:56
memberAmal ElHosseiny2-Feb-06 1:56 
GeneralRe: Problem Pin
ash72-Feb-06 19:57
memberash72-Feb-06 19:57 
GeneralRe: Problem Pin
Amal ElHosseiny3-Feb-06 1:01
memberAmal ElHosseiny3-Feb-06 1:01 
GeneralRe: Problem Pin
big1975foot24-Aug-06 8:21
memberbig1975foot24-Aug-06 8:21 
AnswerRe: Problem Pin
Amal ElHosseiny26-Aug-06 8:10
memberAmal ElHosseiny26-Aug-06 8:10 
GeneralUse a space instead of @ Pin
gomarau31-Jan-06 14:38
membergomarau31-Jan-06 14:38 
GeneralRe: Use a space instead of @ Pin
jefight14-May-07 10:49
memberjefight14-May-07 10:49 
JokeThanx Pin
bestbad29-Jan-06 2:17
memberbestbad29-Jan-06 2:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 26 Jan 2006
Article Copyright 2006 by Amal ElHosseiny
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid