Export (0) Print
Expand All
12 out of 18 rated this helpful - Rate this topic

Installer Class

Provides the foundation for custom installations.

Namespace: System.Configuration.Install
Assembly: System.Configuration.Install (in system.configuration.install.dll)

public class Installer : Component
public class Installer extends Component
public class Installer extends Component
Not applicable.

This is the base class for all custom installers in the .NET Framework. Installers are components that help install applications on a computer.

There are several steps you must follow to use an Installer:

  • Inherit the Installer class.

  • Override the Install, Commit, Rollback, and Uninstall methods.

  • Add the RunInstallerAttribute to your derived class and set it to true.

  • Put your derived class in the assembly with your application to install.

  • Invoke the installers. For example, use the InstallUtil.exe to invoke the installers.

The Installers property contains a collection of installers. If this instance of Installer is part of an installer collection, the Parent property is set to the Installer instance that contains the collection. For an example of the use of the Installers collection, see the AssemblyInstaller class.

The Install, Commit, Rollback, and Uninstall methods of the Installer class go through the collection of installers stored in the Installers property, and invokes the corresponding method of each installer.

The Install, Commit, Rollback, and Uninstall methods are not always called on the same Installer instance. For example, one Installer instance might be used while installing and committing an application, and then the reference to that instance is released. Later, uninstalling the application creates a reference to a new Installer instance, meaning that the Uninstall method is called by a different instance of Installer. For this reason, in your derived class, do not save the state of a computer in an installer. Instead, use an IDictionary that is preserved across calls and passed into your Install, Commit, Rollback, and Uninstall methods.

Two situations illustrate the need to save information in the state-saver IDictionary. First, suppose that your installer sets a registry key. It should save the key's original value in the IDictionary. If the installation is rolled back, the original value can be restored. Second, suppose the installer replaces an existing file. Save the existing file in a temporary directory and the location of the new location of the file in the IDictionary. If the installation is rolled back, the newer file is deleted and replaced by the original from the temporary location.

The Installer.Context property contains information about the installation. For example, information about the location of the log file for the installation, the location of the file to save information required by the Uninstall method, and the command line that was entered when the installation executable was run.

The following example demonstrates the use of the Installer class. It creates a class which inherits from Installer. When Commit is about to complete, Committing event occurs and a message is displayed.

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;

// Set 'RunInstaller' attribute to true.
[RunInstaller(true)]
public class MyInstallerClass: Installer
{
   public MyInstallerClass() :base()
   {
      // Attach the 'Committed' event.
      this.Committed += new InstallEventHandler(MyInstaller_Committed);
      // Attach the 'Committing' event.
      this.Committing += new InstallEventHandler(MyInstaller_Committing);

   }
   // Event handler for 'Committing' event.
   private void MyInstaller_Committing(object sender, InstallEventArgs e)
   {
      Console.WriteLine("");
      Console.WriteLine("Committing Event occured.");
      Console.WriteLine("");
   }
   // Event handler for 'Committed' event.
   private void MyInstaller_Committed(object sender, InstallEventArgs e)
   {
      Console.WriteLine("");
      Console.WriteLine("Committed Event occured.");
      Console.WriteLine("");
   }
   // Override the 'Install' method.
   public override void Install(IDictionary savedState)
   {
      base.Install(savedState);
   }
   // Override the 'Commit' method.
   public override void Commit(IDictionary savedState)
   {
      base.Commit(savedState);
   }
   // Override the 'Rollback' method.
   public override void Rollback(IDictionary savedState)
   {
      base.Rollback(savedState);
   }
   public static void Main()
   {
      Console.WriteLine("Usage : installutil.exe Installer.exe ");
   }
}

import System.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Configuration.Install.*;

// Set 'RunInstaller' attribute to true.
/** @attribute RunInstaller(true)
 */
public class MyInstallerClass extends Installer
{
    public MyInstallerClass()
    {
        // Attach the 'Committed' event.
        this.add_Committed(new InstallEventHandler(MyInstaller_Committed));

        // Attach the 'Committing' event.
        this.add_Committing(new InstallEventHandler(MyInstaller_Committing));
    } //MyInstallerClass

    // Event handler for 'Committing' event.
    private void MyInstaller_Committing(Object sender, InstallEventArgs e)
    {
        Console.WriteLine("");
        Console.WriteLine("Committing Event occured.");
        Console.WriteLine("");
    } //MyInstaller_Committing

    // Event handler for 'Committed' event.
    private void MyInstaller_Committed(Object sender, InstallEventArgs e)
    {
        Console.WriteLine("");
        Console.WriteLine("Committed Event occured.");
        Console.WriteLine("");
    } //MyInstaller_Committed

    // Override the 'Install' method.
    public void Install(IDictionary savedState)
    {
        super.Install(savedState);
    } //Install

    // Override the 'Commit' method.
    public void Commit(IDictionary savedState)
    {
        super.Commit(savedState);
    } //Commit

    // Override the 'Rollback' method.
    public void Rollback(IDictionary savedState)
    {
        super.Rollback(savedState);
    } //Rollback

    public static void main(String[] args)
    {
        Console.WriteLine("Usage : installutil.exe Installer.exe ");
    } //main
} //MyInstallerClass

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.