Blog Stats
  • Posts - 15
  • Articles - 0
  • Comments - 27
  • Trackbacks - 35

 

Changing web.config on install

The install project for a web application is very easy to create and quite effective. There are a lot of options that allows creating directories, shortcuts, registry keys and more, but there are also a few issues that could be important but are not included in project setup. In this and next two or three posts I will show some simple pieces of code that can make delivery of a web application easier.

In the first approach we will take the web.config file on board. The web.config is extremely useful configuration container as everyone probably already knows. Usually in out development and/or test environment we are using different settings that are used in production. Moreover, very often we want to allow our customers to set some settings or even read these settings from their environment. No matter what is applicable in your case, you probably need to change the web.config file during install.

I assume in this moment, that you already have an install project added to your solution and configured according to needs. To get access to files after install process you can override Install method and then by using Context parameters you can get the directory where the application is installed in. Further part is easy than. Using standard I/O methods you can access to any file in the freshly installed application and do everything what you want with them. As you can see in snippet below, after reading the install directory install is loading the web.config file as an XML and then modifying connection string.

    public override void Install(System.Collections.IDictionary stateSaver)
    {
      base.Install(stateSaver);
      string installPath = Context.Parameters["dir"];
      installPath += "web.config";
      SetWebConfig(installPath);
    }

    private void SetWebConfig(string path)
    {
      XmlReaderSettings rSet = new XmlReaderSettings();
      rSet.CloseInput = true;
      XmlReader reader = XmlReader.Create(path, rSet);
      string contents = reader.ReadOuterXml();
      XmlDocument cnf = new XmlDocument();
      cnf.Load(reader);
      reader.Close();
      foreach (XmlNode confNode in cnf.DocumentElement.ChildNodes)
      {
        if (confNode.Name == "connectionStrings")
          foreach (XmlNode cnNode in confNode.ChildNodes)
          {
            if (cnNode.Name == "add" && cnNode.Attributes["connectionString"] != null)
            {
              string connStr = cnNode.Attributes["connectionString"].Value.ToString();
              string newConnStr = "Data Source=ProdServer;Initial Catalog=ProdDatabase;Integrated Security=True;";
              cnNode.Attributes["connectionString"].Value = newConnStr;
            }
          }
      }
      XmlWriterSettings wSet = new XmlWriterSettings();
      wSet.Indent = true;
      wSet.CloseOutput = true;
      XmlWriter writer = XmlWriter.Create(path, wSet);
      cnf.Save(writer);
      writer.Close();
    }

Cheers


Feedback

# re: Changing web.config on install

Gravatar It doesn't work. :(

I get error during installation - "The process cannot access the file web.config because it is being used by another process."

Help me !!! 6/29/2006 10:24 AM | Denis

# re: Changing web.config on install

Gravatar I had the same problem if there was no

base.Install(stateSaver);

at the beginning of Install method. Before install will finish, all files are locked. If you can, send me your sources and will try to help.
6/29/2006 11:08 AM | Lazy Developer

Post a comment





 

 

 

Copyright © Ziemowit 'Jimmy' Skowronski