CodeSnip: Modifying Web.config file during installation
page 1 of 1
Published: 02 May 2006
Unedited - Community Contributed
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.
by Amal ElHosseiny
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 12734/ 28

In this article, I will demonstrate how to modify web.config file for web a service project during installation dynamically according to user entries.

Download Source


Figure 1

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 to store data that can be changed dynamically by the user.

Listing 1: Web.Config

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

A user may have to open and change this file every time the web application or service is moved from server to another. To avoid it, in the installer I've enabled a facility for user to change the xml attributes values.

Listing 2: Web.Config

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

In this article, I will change the value for <appsettings> attribute by implementing installer class added to web service project.

For that first from the project I will add a new item as shown in figure 2.

Figure 2

From Add New Item screen I will select Installer Class as shown in figure 3.

Figure 3

Then in the installer class, I will override the Install function as shown in listing 3.

Listing 3: Install () in Installer.cs

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

To get data required to change the values in web.config from the user, I will add a custom action in the setup project as shown in figure 4.

Figure 4

I will then change the custom data property for it as shown in listing 5.

Listing 5:Custom Action Installer


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

Figure 5

You will notice the value here:


I've separated the parameters to the Installer class by a special letter (@).

Listing 6 :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:

Listing 7: void CreateConnectionString()

    Assembly ass=Assembly.GetExecutingAssembly();
    EventLog.WriteEntry ("Web.Config",ass.GetName().Name+".Web.config");
    Stream stmConfig=ass.GetManifestResourceStream(                 ass.GetName().Name+".Web.config");
    FileStream stmPhysical=new FileStream(
    StreamReader srConfig=newStreamReader(stmConfig);
    StreamWriter swConfig=newStreamWriter(stmPhysical);
    string strConfig=srConfig.ReadToEnd();
   strConfig=strConfig.Replace("server=(local);database" + 
              "=DatabaseName;UserID=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 web service is installed in. In this case, it is:


Download Source


In this article, you have learned how to change any attributes values in web.config. From onwards IIS6 we can change web.config files dynamically using ASP.NET MMS snap-in. If you are curious to know more about it, you can check link.


User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-24 10:33:16 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search