Click here to Skip to main content
Licence 
First Posted 26 Jan 2006
Views 87,236
Downloads 340
Bookmarked 77 times

Modifying Web.Config During Installation

By | 26 Jan 2006 | Article
How to modify the Web.Config of a WebService during installation.

Abstract

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.

Explanation

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.

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

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.

<appSettings>
  <add key="Main.ConnectionString" 
        value="server=(local);database=DatabaseName;
               User ID=sa;Password=;
               trusted_connection=false"/>
</appSettings>

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
    CreateConnectionString();
    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:

/Server=[EDITA1]@/Username=
      [EDITA2]@/Password=[EDITA3]@/Folder=[EDITA4]

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

You will notice the value here:

/Server=[EDITA1]@/Username=[EDITA2]@/Password=[EDITA3]@/Folder=[EDITA4]

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"]);

    strServer=Context.Parameters["Server"].Split('@')[0];
    strUser=Context.Parameters["Server"].Split('@')[1];
    strPassword=Context.Parameters["Server"].Split('@')[2];
    strFolder=Context.Parameters["Server"].Split('@')[3];

    Context.Parameters["Server"]=strServer;
    Context.Parameters["Username"]=strUser.Split('=')[1];
    EventLog.WriteEntry("user",Context.Parameters["Username"]);

    Context.Parameters["Password"]=strPassword.Split('=')[1];
    EventLog.WriteEntry("Password",Context.Parameters["Password"]);

    Context.Parameters["Folder"]=strFolder.Split('=')[1];
    EventLog.WriteEntry("folder",Context.Parameters["Folder"]);

    EventLog.WriteEntry("Server",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()
{
    FormateParamters();

    Assembly ass=Assembly.GetExecutingAssembly();
    EventLog.WriteEntry ("Web.Config", 
             ass.GetName().Name+".Web.config");

    Stream stmConfig=ass.GetManifestResourceStream(
                   ass.GetName().Name+".Web.config");

    if(!Directory.Exists(Context.Parameters["Folder"]))
        Directory.CreateDirectory(Context.Parameters["Folder"]);

    FileStream stmPhysical=new FileStream(
        Context.Parameters["Folder"]+@"\Web.config",
        FileMode.Create);
    StreamReader srConfig=new StreamReader(stmConfig);
    StreamWriter swConfig=new StreamWriter(stmPhysical);

    string strConfig=srConfig.ReadToEnd();
    stmConfig.Close();
    strConfig=strConfig.Replace("server=(local);database" + 
              "=DatabaseName;User ID=sa;Password=;" + 
              "trusted_connection=false",NewConnection());


    swConfig.Write(strConfig);
    swConfig.Close ();
    stmPhysical.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:

C:\intpub\wwwroot\webtest1\

License

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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCreate Web Service Installer PinmemberMember 38889640:58 4 May '09  
GeneralGreat article! PinmemberThomas ST3:02 21 Apr '09  
GeneralPossible better way to acquire web.config path PinmemberBillyGoatGruff5:22 8 Apr '09  
GeneralRe: Possible better way to acquire web.config path [modified] PinmemberThomas ST2:57 21 Apr '09  
QuestionModifing the Project for the Installer??? PinmemberRicardo Casquete16:16 3 Feb '08  
Generalproblem in installation Pinmemberchinni10000718:56 19 Sep '07  
GeneralRe: problem in installation Pinmemberanand.lv2:26 16 Jan '09  
GeneralRe: Problems when comments in web.config <!-- Some Comment --> PinmemberAmal ElHosseiny23:18 29 Mar '07  
GeneralRe: Problems when comments in web.config &lt;!-- Some Comment --> Pinmemberassadhf1:11 30 Mar '07  
GeneralSuggestion Pinmembersyemhusa23:42 21 Nov '06  
GeneralLocal folder-path Pinmemberedgecrush3r0:46 27 Sep '06  
Generalsorry ... broken download Pinmembermarquito_cuba6:18 27 Jul '06  
GeneralRe: sorry ... broken download PinmemberAmal ElHosseiny20:59 30 Jul '06  
GeneralUse of EventLog.WriteEntry In this article PinmemberAuldrin John Possa16:23 22 Feb '06  
GeneralRe: Use of EventLog.WriteEntry In this article PinmemberAmal ElHosseiny22:26 3 Mar '06  
GeneralVery useful Pinmembermosessaur3:05 13 Feb '06  
GeneralWeb Application Folder Pinmemberash723:57 5 Feb '06  
GeneralNice Job PinmemberBrent Finney8:16 2 Feb '06  
GeneralProblem Pinmemberash70:51 2 Feb '06  
GeneralRe: Problem PinmemberAmal ElHosseiny0:56 2 Feb '06  
GeneralRe: Problem Pinmemberash718:57 2 Feb '06  
GeneralRe: Problem PinmemberAmal ElHosseiny0:01 3 Feb '06  
GeneralRe: Problem Pinmemberbig1975foot7:21 24 Aug '06  
AnswerRe: Problem PinmemberAmal ElHosseiny7:10 26 Aug '06  
GeneralUse a space instead of @ Pinmembergomarau13:38 31 Jan '06  

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

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

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