InstallContext Class

Contains information about the current installation.

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

'Declaration
Public Class InstallContext
'Usage
Dim instance As InstallContext

public class InstallContext
public class InstallContext

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.

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


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

/** @attribute RunInstallerAttribute(true)
 */
class InstallContextExample extends Installer
{
    public InstallContext myInstallContext;

    public void Install(IDictionary mySavedState)
    {
        StringDictionary myStringDictionary = myInstallContext.get_Parameters();
        if (myStringDictionary.get_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.
     //Install
    
    public void Uninstall(IDictionary mySavedState)
    {
        // The 'Uninstall' procedure should be added here.
     //Uninstall
    
    public void Rollback(IDictionary mySavedState)
    {
        if (myInstallContext.IsParameterTrue("LogtoConsole") == true) {
            myInstallContext.LogMessage(
                "The 'Rollback' method has been called");

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

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

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

    public static void main(String[] args)
    {
        InstallContextExample myInstallObject = new InstallContextExample();
        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.set_Item(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 (System.Exception exp) {
            // Call the 'Rollback' method.
            myInstallObject.Rollback(mySavedState);
        
     //main
 //InstallContextExample

System.Object
  System.Configuration.Install.InstallContext

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show: