InstallContext Class

Definition

Contains information about the current installation.

C#
public class InstallContext
Inheritance
InstallContext

Examples

The following example demonstrates the InstallContext constructors, the Parameters property and the LogMessage and IsParameterTrue methods of the InstallContext class.

When the Install method of the installer is called, it checks for parameters from the command line. Depending on that, it displays the progress messages onto the console and also saves it to the specified log file.

When the program is invoked without any arguments, an empty InstallContext is created. When "/LogFile" and "/LogtoConsole" are specified, the InstallContext is created by passing the respective arguments to InstallContext.

C#
using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.Collections;
using System.Collections.Specialized;

namespace MyInstallContextNamespace
{
   [RunInstallerAttribute(true)]
   class InstallContext_Example : Installer
   {
      public InstallContext myInstallContext;

      public override void Install( IDictionary mySavedState )
      {
         base.Install( mySavedState );
         StringDictionary myStringDictionary = myInstallContext.Parameters;
         if( myStringDictionary.Count == 0 )
         {
            Console.WriteLine( "No parameters have been entered in the command line "
               +"hence, the install will take place in the silent mode" );
         }
         else
         {
            // Check whether the "LogtoConsole" parameter has been set.
            if ( myInstallContext.IsParameterTrue( "LogtoConsole" ))
            {
               // Display the message to the console and add it to the logfile.
               myInstallContext.LogMessage( "The 'Install' method has been called" );
            }
         }

         // The 'Install procedure should be added here.
      }

      public override void Uninstall( IDictionary mySavedState )
      {
         base.Uninstall( mySavedState );
         // The 'Uninstall' procedure should be added here.
      }

      public override void Rollback( IDictionary mySavedState )
      {
         base.Rollback( mySavedState );
         if ( myInstallContext.IsParameterTrue( "LogtoConsole" ))
         {
            myInstallContext.LogMessage( "The 'Rollback' method has been called" );
         }

         // The 'Rollback' procedure should be added here.
      }

      public override void Commit( IDictionary mySavedState )
      {
         base.Commit( mySavedState );
         if ( myInstallContext.IsParameterTrue( "LogtoConsole" ))
         {
            myInstallContext.LogMessage( "The 'Commit' method has been called" );
         }

         // The 'Commit' procedure should be added here.
      }

      static void Main( string[] args )
      {
         InstallContext_Example myInstallObject = new InstallContext_Example();

         IDictionary mySavedState = new Hashtable();

         if( args.Length < 1 )
         {
            // There are no command line arguments, create an empty 'InstallContext'.
            myInstallObject.myInstallContext = new InstallContext();
         }

         else if( ( args.Length == 1 ) && ( args[ 0 ] == "/?" ) )
         {
            // Display the 'Help' for this utility.
            Console.WriteLine( "Specify the '/Logfile' and '/LogtoConsole' parameters" );
            Console.WriteLine( "Example: " );
            Console.WriteLine( "InstallContext_InstallContext.exe /LogFile=example.log"
                                          +" /LogtoConsole=true" );
            return;
         }

         else
         {
            // Create an InstallContext object with the given parameters.
            String[] commandLine = new string[ args.Length ];
            for( int i = 0; i < args.Length; i++ )
            {
               commandLine[ i ] = args[ i ];
            }
            myInstallObject.myInstallContext = new InstallContext( args[ 0 ], commandLine);
         }

         try
         {
            // Call the 'Install' method.
            myInstallObject.Install( mySavedState );

            // Call the 'Commit' method.
            myInstallObject.Commit( mySavedState );
         }
         catch( Exception )
         {
            // Call the 'Rollback' method.
            myInstallObject.Rollback( mySavedState );
         }
      }
   }
}

Remarks

Typically, an InstallContext is created by an installation executable, such as InstallUtil.exe, that installs assemblies. The installation program invokes the InstallContext constructor, passing it the default log-file path and command-line parameters.

Prior to calling its Install, Commit, Rollback, or Uninstall methods, the installation program sets the Context property of an Installer to the instance of InstallContext. Before calling these methods, an Installer that contains an installer collection in the Installers property sets the Context property of each contained installer.

The Parameters property contains a parsed version of the command line that is entered to run the installation executable. The property contains information such as the path to a log file, whether to display log information on the console, and whether to show a user interface during the installation. Call the IsParameterTrue method to find out whether a command-line parameter is true.

Use the LogMessage method to write status messages to the installation log file and the console.

Constructors

InstallContext()

Initializes a new instance of the InstallContext class.

InstallContext(String, String[])

Initializes a new instance of the InstallContext class, and creates a log file for the installation.

Properties

Parameters

Gets the command-line parameters that were entered when InstallUtil.exe was run.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsParameterTrue(String)

Determines whether the specified command-line parameter is true.

LogMessage(String)

Writes a message to the console and to the log file for the installation.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ParseCommandLine(String[])

Parses the command-line parameters into a string dictionary.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also