Este tema aún no ha recibido ninguna valoración - Valorar este tema

InstallContext (Clase)

Contiene información sobre la actual instalación.

Espacio de nombres: System.Configuration.Install
Ensamblado: System.Configuration.Install (en system.configuration.install.dll)

public class InstallContext
public class InstallContext
public class InstallContext

Normalmente, se crea una clase InstallContext mediante un ejecutable de instalación, como InstallUtil.exe, que instala ensamblados. El programa de instalación invoca al constructor InstallContext, pasándole la ruta de acceso predeterminada al archivo de registro y los parámetros de la línea de comandos.

Antes de llamar a sus métodos Install, Commit, Rollback o Uninstall, el programa de instalación establece la propiedad Context de Installer en la instancia de InstallContext. Antes de llamar a estos métodos, un objeto Installer que contiene una colección de instaladores en la propiedad Installers establece la propiedad Context de cada instalador que contiene.

La propiedad Parameters contiene una versión analizada de la línea de comandos que se especifica para ejecutar el ejecutable de instalación. La propiedad contiene información como la ruta de acceso a un archivo de registro, si se va a mostrar información de registro en la consola y si se va a mostrar una interfaz de usuario durante la instalación. Hay que llamar al método IsParameterTrue para averiguar si un parámetro de la línea de comandos es true.

Hay que utilizar el método LogMessage para escribir mensajes de estado en el archivo de registro de la instalación y la consola.

En el siguiente ejemplo se muestran los constructores InstallContext, la propiedad Parameters y los métodos LogMessage y IsParameterTrue de la clase InstallContext.

Cuando se llama al método Install del instalador, éste comprueba los parámetros especificados en la línea de comandos. Dependiendo de esos parámetros, muestra los mensajes indicadores de progreso en la consola y los guarda en el archivo de registro especificado.

Cuando se invoca al programa sin argumentos, se crea un InstallContext vacío. Cuando se especifican "/LogFile" y "/LogtoConsole", el InstallContext se crea pasando los argumentos respectivos a 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 );
         }
      }
   }
}

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
Los miembros estáticos públicos (Shared en Visual Basic) de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.

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

.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.

.NET Framework

Compatible con: 2.0, 1.1, 1.0
¿Le ha resultado útil?
(Caracteres restantes: 1500)
Contenido de la comunidad Agregar