Export (0) Print
Expand All

InstallContext Class

Contains information about the current installation.

For a list of all members of this type, see InstallContext Members.

System.Object
   System.Configuration.Install.InstallContext

[Visual Basic]
Public Class InstallContext
[C#]
public class InstallContext
[C++]
public __gc class InstallContext
[JScript]
public class InstallContext

Thread Safety

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

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.

Example

[Visual Basic, C#, C++] The following example demonstrates the InstallContext constructors, the Parameters property and the LogMessage and IsParameterTrue methods of the InstallContext class.

[Visual Basic, C#, C++] 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.

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Imports System
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.Collections
Imports System.Collections.Specialized

Namespace MyInstallContextNamespace
   <RunInstallerAttribute(True)> Class InstallContext_Example
      Inherits Installer
      Public myInstallContext As InstallContext

      Public Overrides Sub Install(mySavedState As IDictionary)
         Dim myStringDictionary As StringDictionary = myInstallContext.Parameters
         If myStringDictionary.Count = 0 Then
            Console.WriteLine("No parameters have been entered in the command line" + _
                        "hence, the install will take place in the silent mode")
         Else
            ' Check wether the "LogtoConsole" parameter has been set.
            If myInstallContext.IsParameterTrue("LogtoConsole") = True Then
               ' Display the message to the console and add it to the logfile.
               myInstallContext.LogMessage("The 'Install' method has been called")
            End If
         End If
         ' The 'Install procedure should be added here.
      End Sub 'Install

      Public Overrides Sub Uninstall(mySavedState As IDictionary)
         ' The 'Uninstall' procedure should be added here.
      End Sub 'Uninstall

      Public Overrides Sub Rollback(mySavedState As IDictionary)
         If myInstallContext.IsParameterTrue("LogtoConsole") = True Then
            myInstallContext.LogMessage("The 'Rollback' method has been called")
         End If
         ' The 'Rollback' procedure should be added here.
      End Sub 'Rollback

      Public Overrides Sub Commit(mySavedState As IDictionary)
         If myInstallContext.IsParameterTrue("LogtoConsole") = True Then
            myInstallContext.LogMessage("The 'Commit' method has been called")
         End If
         ' The 'Commit' procedure should be added here.
      End Sub 'Commit

      ' Entry point which delegates to C-style main Private Function
      Public Overloads Shared Sub Main()
         Main(System.Environment.GetCommandLineArgs())
      End Sub

      Overloads Shared Sub Main(args() As String)
         Dim myInstallObject As New InstallContext_Example()
         Dim mySavedState = New Hashtable()

         If args.Length < 2 Then
            ' There are no command line arguments, create an empty 'InstallContext'.
            myInstallObject.myInstallContext = New InstallContext()
         ElseIf args.Length = 2 And args(1) = "/?" Then
               ' 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.
            Dim commandLine() As String = New String(args.Length - 2) {}
            Dim i As Integer
            For i = 1 To args.Length - 1
               commandLine(i-1) = args(i)
            Next i
            myInstallObject.myInstallContext = _
               New InstallContext("/LogFile:example.log", commandLine)
         End If

         Try
            ' Call the 'Install' method.
            myInstallObject.Install(mySavedState)

            ' Call the 'Commit' method.
            myInstallObject.Commit(mySavedState)
         Catch
            ' Call the 'Rollback' method.
            myInstallObject.Rollback( mySavedState )
         End Try
      End Sub 'Main
   End Class 'InstallContext_Example
End Namespace 'MyInstallContextNamespace


[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 )
      {
         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 );
         }
      }
   }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Configuration.Install.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Configuration::Install;
using namespace System::Collections;
using namespace System::Collections::Specialized;

[RunInstallerAttribute(true)]
__gc class InstallContext_Example : public Installer {
public:
    InstallContext* myInstallContext;

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

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

    void Uninstall(IDictionary* mySavedState) {
        // The 'Uninstall' procedure should be added here.
    }

    void Rollback(IDictionary* mySavedState) {
        if (myInstallContext->IsParameterTrue(S"LogtoConsole")) {
            myInstallContext->LogMessage(S"The 'Rollback' method has been called");
        }

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

    void Commit(IDictionary* mySavedState) {
        if (myInstallContext->IsParameterTrue(S"LogtoConsole")) {
            myInstallContext->LogMessage(S"The 'Commit' method has been called");
        }

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

int main() {
    String* args[] = Environment::GetCommandLineArgs();
    InstallContext_Example* myInstallObject = new InstallContext_Example();

    IDictionary* mySavedState = new Hashtable();

    if (args->Length < 2) {
        // There are no command line arguments, create an empty 'InstallContext'.
        myInstallObject->myInstallContext = new InstallContext();
    } else if ((args->Length == 2) && (args[1]->Equals(S"/?"))) {
        // Display the 'Help' for this utility.
        Console::WriteLine(S"Specify the '/Logfile' and '/LogtoConsole' parameters");
        Console::WriteLine(S"Example: ");
        Console::WriteLine(S"InstallContext_InstallContext.exe /LogFile=example.log  /LogtoConsole=true");
        return 0;
    } else {
        // Create an InstallContext object with the given parameters.
        String* commandLine[] = new String*[args->Length - 1];
        for (int i = 0; i < args->Length - 1; i++) {
            commandLine->Item[i] = args->Item[i + 1];
        }
        myInstallObject->myInstallContext = new InstallContext(args[1], 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);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Configuration.Install

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

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

See Also

InstallContext Members | System.Configuration.Install Namespace | Installer | TransactedInstaller | AssemblyInstaller

Show:
© 2014 Microsoft