InstallContext Klasse

Definition

Enthält Informationen über die derzeitige Installation.

public ref class InstallContext
public class InstallContext
type InstallContext = class
Public Class InstallContext
Vererbung
InstallContext

Beispiele

Im folgenden Beispiel werden die InstallContext Konstruktoren, die Parameters -Eigenschaft und die LogMessage Methoden und IsParameterTrue der InstallContext -Klasse veranschaulicht.

Wenn die Install -Methode des Installationsprogramms aufgerufen wird, wird über die Befehlszeile nach Parametern gesucht. Abhängig davon werden die Statusmeldungen auf der Konsole angezeigt und auch in der angegebenen Protokolldatei gespeichert.

Wenn das Programm ohne Argumente aufgerufen wird, wird ein leerer InstallContext erstellt. Wenn "/LogFile" und "/LogtoConsole" angegeben werden, wird erstellt InstallContext , indem die entsprechenden Argumente an InstallContextübergeben werden.

#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)]
ref class InstallContext_Example: public Installer
{
public:
   InstallContext^ myInstallContext;
   virtual void Install( IDictionary^ mySavedState ) override
   {
      StringDictionary^ myStringDictionary = myInstallContext->Parameters;
      if ( myStringDictionary->Count == 0 )
      {
         Console::Write( "No parameters have been entered in the command line " );
         Console::WriteLine( "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.
   }

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

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

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

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

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

int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   InstallContext_Example^ myInstallObject = gcnew InstallContext_Example;
   IDictionary^ mySavedState = gcnew Hashtable;
   if ( args->Length < 2 )
   {
      // There are no command line arguments, create an empty 'InstallContext'.
      myInstallObject->myInstallContext = gcnew InstallContext;
   }
   else
   if ( (args->Length == 2) && (args[ 1 ]->Equals( "/?" )) )
   {
      // 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 0;
   }
   else
   {
      // Create an InstallContext object with the given parameters.
      array<String^>^commandLine = gcnew array<String^>(args->Length - 1);
      for ( int i = 0; i < args->Length - 1; i++ )
      {
         commandLine[ i ] = args[ i + 1 ];
      }
      myInstallObject->myInstallContext = gcnew 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 );
   }
}
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" ) == 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 )
      {
         base.Uninstall( mySavedState );
         // The 'Uninstall' procedure should be added here.
      }

      public override void Rollback( IDictionary mySavedState )
      {
         base.Rollback( 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 )
      {
         base.Commit( 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 );
         }
      }
   }
}
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

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

      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

      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

      ' 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
   End Class
End Namespace 'MyInstallContextNamespace

Hinweise

In der Regel wird von InstallContext einer ausführbaren Installationsdatei wie InstallUtil.exe erstellt, die Assemblys installiert. Das Installationsprogramm ruft den InstallContext Konstruktor auf und übergibt ihn den Standardprotokollpfad und die Befehlszeilenparameter.

Vor dem Aufrufen der InstallMethoden , Commit, Rollbackoder Uninstall legt das Installationsprogramm die Context -Eigenschaft von Installer auf die -Instanz von InstallContextfest. Vor dem Aufrufen dieser Methoden legt eine Installer , die eine Installerauflistung in der Installers -Eigenschaft enthält, die -Eigenschaft der Context einzelnen enthaltenen Installer fest.

Die Parameters -Eigenschaft enthält eine analysierte Version der Befehlszeile, die zum Ausführen der ausführbaren Installationsdatei eingegeben wird. Die -Eigenschaft enthält Informationen wie den Pfad zu einer Protokolldatei, ob Protokollinformationen in der Konsole angezeigt werden sollen und ob während der Installation eine Benutzeroberfläche angezeigt werden soll. Rufen Sie die IsParameterTrue -Methode auf, um herauszufinden, ob ein Befehlszeilenparameter ist true.

Verwenden Sie die LogMessage -Methode, um Statusmeldungen in die Installationsprotokolldatei und die Konsole zu schreiben.

Konstruktoren

InstallContext()

Initialisiert eine neue Instanz der InstallContext-Klasse.

InstallContext(String, String[])

Initialisiert eine neue Instanz der InstallContext-Klasse und erstellt eine Protokolldatei für die Installation.

Eigenschaften

Parameters

Ruft die Befehlszeilenparameter ab, die beim Ausführen von InstallUtil.exe eingegeben wurden.

Methoden

Equals(Object)

Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist.

(Geerbt von Object)
GetHashCode()

Fungiert als Standardhashfunktion.

(Geerbt von Object)
GetType()

Ruft den Type der aktuellen Instanz ab.

(Geerbt von Object)
IsParameterTrue(String)

Bestimmt, ob der angegebene Befehlszeilenparameter true ist.

LogMessage(String)

Gibt eine Meldung an die Konsole und in die Protokolldatei für die Installation aus.

MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.

(Geerbt von Object)
ParseCommandLine(String[])

Liest die Befehlszeilenparameter in ein Zeichenfolgenwörterbuch ein.

ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.

(Geerbt von Object)

Gilt für:

Weitere Informationen