Share via


Walkthrough: Passing Data to a Custom Action

The following walkthrough demonstrates passing data to a dynamic property during installation using a custom action, an installer class, and the CustomActionData property.

To create the custom action

  1. On the File menu, point to New, and then choose Project.
  2. In the New Project dialog box, select Visual Basic Projects in the Project Type pane, and then choose Windows Application in the Templates pane. In the Name box, type PassData.
  3. Click OK to close the dialog box.
  4. Select the form in the Form Designer. In the Toolbox, select a TextBox control and add it to the form.
  5. Select the TextBox control. In the Properties window, select DynamicProperties, and expand the DynamicProperties node.
  6. Click the Ellipsis () button next to the Advanced property, and perform these steps in the dialog box that appears:
    1. In the Properties list box, select the check box next to the Text property.
    2. In the Key Mapping combo box, type ServerName.
  7. Click OK to close the dialog box.

To create an installer class

  1. Select the PassData project in Solution explorer. On the Project menu, choose Add New Item.

  2. In the Add New Item dialog box, choose Installer Class.

  3. Click OK to close the dialog box.

  4. Add the following procedure to override the Install procedure of the base class:

    Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
       MyBase.Install(stateSaver)
       ' Gets the parameter passed across in the CustomActionData.
       Dim ProvidedName As String = Me.Context.Parameters.Item("ServerName")
    
       If ProvidedName = "" Then
          Throw New InstallException("No arguments specified")
       End If
    
       ' Uses reflection to find the location of the config file.
       Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
       Dim FileInfo As System.IO.FileInfo = New System.IO.FileInfo(Asm.Location + ".config")
       If Not FileInfo.Exists Then
          Throw New InstallException("Missing config file")
       End If
    
       ' Loads the config file into the XML DOM.
       Dim XmlDocument As New System.Xml.XmlDocument()
       XmlDocument.Load(FileInfo.FullName)
    
       ' Finds the right node and change it to the new value.
       Dim Node As System.Xml.XmlNode
       Dim FoundIt As Boolean = False
       For Each Node In XmlDocument.Item("configuration").Item("appSettings")
          If Node.Name = "add" Then ' skip any comments
             If Node.Attributes.GetNamedItem("key").Value = "ServerName" Then
                Node.Attributes.GetNamedItem("value").Value = ProvidedName
                FoundIt = True
             End If
          End If
       Next Node
    
       If Not FoundIt Then
          Throw New InstallException("Config file did not contain a ServerName section")
       End If
    
       ' Write out the new config file.
       XmlDocument.Save(FileInfo.FullName)
    End Sub
    

To create a deployment project

  1. On the File menu, choose Add Project, New Project.
  2. In the Add Project dialog box, select Setup and Deployment Projects in the Project Type pane, and then choose Setup Project in the Templates pane. In the Name box, type PassData Installer.
  3. Click OK to close the dialog box.
  4. In the Properties window, select the ProductName property and type PassData.
  5. Select the Manufacturer property and type My Company.
  6. In the File System Editor, select the Application Folder node. On the Action menu, choose Add, Project Output.
  7. In the Add Project Output Group dialog box, select the primary output for the PassData project. Click OK to close the dialog box.

To add a custom action

  1. Select the PassData Installer project in Solution Explorer. On the View menu, point to Editor, and choose Custom Actions.

  2. In the Custom Actions Editor, select the Custom Actions node. On the Action menu, choose Add Custom Action.

  3. In the Select item in project dialog box, double-click the Application Folder.

  4. Select the Primary output from PassData (Active) item.

  5. Click OK to close the dialog box.

    This will add the PassData custom action to all four custom action nodes.

  6. Select the Primary output from PassData (Active) node beneath the Install node.

  7. In the Properties window, select the CustomActionData property and type /ServerName=[EDITA1].

  8. Repeat steps 6 and 7 for each of the other three instances of Primary output from PassData (Active).

To customize the installation user interface

  1. Select the Setup project in Solution Explorer. On the View menu, point to Editor, and choose User Interface.
  2. In the User Interface Editor, select the Start node under Install. On the Action menu, choose Add Dialog.
  3. In the Add Dialog dialog box, select the Textboxes (A) dialog.
  4. Click OK to close the dialog box.
  5. On the Action menu, choose Move Up. Repeat until the Textboxes (A) dialog is above the Installation Folder node.
  6. In the Properties window, select the BannerText property and type What server do you want to use?.
  7. Select the BodyText property and type Enter the name of the server for the PassData application.
  8. Select the Edit1Label property and type Server Name:.
  9. Select the Edit2Visible, Edit3Visible, and Edit4Visible properties and set them to false.
  10. On the Build menu, choose Build Pass Data Installer.

To install on your development computer

  • Select the PassData Installer project in Solution Explorer. On the Project menu, choose Install.

    This will run the installer on your development computer. In the What server do you want to use? installation dialog box, type MyServer.

    Note   You must have install permissions on the computer in order to run the installer.

To deploy to another computer

  1. In Windows Explorer, navigate to your project directory and find the built installer. The default path will be \documents and settings\yourloginname\PassData Installer\project configuration\PassData Installer.msi. The default project configuration is Debug.

  2. Copy the PassData Installer.msi file and all other files and subdirectories in the directory to another computer.

    Note   To install on a computer that is not on a network, copy the files to traditional media such as CD-ROM.

    On the target computer, double-click the Setup.exe file to run the installer. In the What server do you want to use? installation dialog box, type MyServer.

    Note   You must have install permissions on the computer in order to run the installer.

To test the installation

  • Run the application, and verify that the text box contains the text "MyServer" that was entered during installation.

To uninstall the application

  1. In the Windows Control Panel, double-click Add/Remove Programs.

  2. In the Add/Remove Programs dialog box, select PassData Installer and click Remove, then click OK to close the dialog box.

    Tip   To uninstall from your development computer, on the Project menu, choose Uninstall.

See Also

CustomActionData Property | Custom Actions Management in Deployment | Creating Installation Components