Export (0) Print
Expand All

InstallContext Class

Contains information about the current installation.

System.Object
  System.Configuration.Install.InstallContext

Namespace:  System.Configuration.Install
Assembly:  System.Configuration.Install (in System.Configuration.Install.dll)

public class InstallContext

The InstallContext type exposes the following members.

  NameDescription
Public methodInstallContext()Initializes a new instance of the InstallContext class.
Public methodInstallContext(String, String[])Initializes a new instance of the InstallContext class, and creates a log file for the installation.
Top

  NameDescription
Public propertyParametersGets the command-line parameters that were entered when InstallUtil.exe was run.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsParameterTrueDetermines whether the specified command-line parameter is true.
Public methodLogMessageWrites a message to the console and to the log file for the installation.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodStatic memberParseCommandLineParses the command-line parameters into a string dictionary.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

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.

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 )
      {
         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" ) == true )
            {
               // 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 )
      {
         // The 'Uninstall' procedure should be added here.
      }

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

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

      public override void Commit( IDictionary mySavedState )
      {
         if( myInstallContext.IsParameterTrue( "LogtoConsole" ) == true )
         {
            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 );
         }
      }
   }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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