Installer Projects - Visual Studio

Overview

This article gives an overview of working with Installer projects within Visual Studio. Refer also to Web.Config Transformations with Web Setup Project - Visual Studio.

Installer projects are primarily declarative in nature, with the exception of Installer Classes, which are included in your application code.

ItemHow to Configure
Files to installView menu > Editor > File System
Files NOT to installSolution Explorer > Installer project > "Content Files" or "Primary output" > right-click > Exclude filter
Registry entriesView menu > Editor > Registry
File Types to createView menu > Editor > File Types
Installer dialogsView menu > Editor > User Interface
Custom actionsView menu > Editor > Custom Actions
Launch conditionsView menu > Editor > Launch Conditions
PrerequisitesSolution Explorer > Installer Project > right-click > Properties > Prerequisites button

Walk-Throughs

Changing a Connection String Based on User Input

1. Include a "Textboxes" dialog in your User Interface.

2. Specify the following property values for the Textboxes dialog.

Edit1PropertyTARGETSITE
Edit1Value/LM/W3SVC/1
Edit1VisibleFalse
Edit2PropertyTARGETVDIR
Edit2Value(default value)
Edit2VisibleFalse
Edit3LabelSQL Server Instance
Edit3PropertySQLSERVERINSTANCE
Edit3Value(default value; e.g., ".\SQLEXPRESS")
Edit3VisibleTrue

3. Add a Custom Action on "Primary Output...", and in its CustomActionData property, specify "/Site=[TARGETSITE] /Vdir=[TARGETVDIR] /SqlSvr=[SQLSERVERINSTANCE]". (This will refer to the value in the textboxes from the previous step.)

4. To your application code, add an installer class. Within its override of the the Install method, add the following code.

Dim site As String = Me.Context.Parameters.Item("site")
Dim appName As String = Me.Context.Parameters.Item("vdir")
Dim sqlServerInstance As String = Me.Context.Parameters.Item("sqlsvr")

Dim siteNum as Integer = Integer.Parse(site.Substring(site.LastIndexOf("/") + 1)) - 1
Dim mySite As Microsoft.Web.Administration.Site = mgr.Sites(siteNum)
Dim myApp As Microsoft.Web.Administration.Application = mySite.Applications("/" & appName)
Dim mgr As ServerManager = New ServerManager()
Dim rootPath As String = myApp.VirtualDirectories("/").PhysicalPath
TweakConnectionStringDataSource(rootPath, "Local", sqlServerInstance)

5. Add the following method to your installer class.

Private Shared Sub TweakConnectionStringDataSource(rootPath As String, connectionStringName As String, newDataSource As String)

    Dim configFile As String = Path.Combine(rootPath, "web.config")
    Dim doc As XmlDocument = New XmlDocument()
    doc.Load(configFile)
    Dim xPath As String = "/configuration/connectionStrings/add[@name='" & connectionStringName & "']"
    Dim node As XmlNode = doc.SelectSingleNode(xPath)
    Dim attr As XmlAttribute = node.Attributes("connectionString")
    Dim conn = New SqlConnectionStringBuilder(attr.Value)

    ' We do this tweak because, for some reason, when the user specified ".\SQLEXPRESS" (for example)
    ' it comes in here as ".\\SQLEXPRESS".
    newDataSource = newDataSource.Replace("\\", "\")

    conn.DataSource = newDataSource

    attr.Value = conn.ConnectionString
    Dim newConfigFile = configFile ' Path.Combine(rootPath, "web2.config") ' This is for testing
    doc.Save(newConfigFile)

End Sub