Installer 類別

定義

提供自訂安裝的基礎。Provides the foundation for custom installations.

public ref class Installer : System::ComponentModel::Component
public class Installer : System.ComponentModel.Component
type Installer = class
    inherit Component
Public Class Installer
Inherits Component
繼承
衍生

範例

下列範例示範Installer類別的用法。The following example demonstrates the use of the Installer class. 它會建立繼承自Installer的類別。It creates a class which inherits from Installer. Commit即將完成時, Committing就會發生事件並顯示訊息。When Commit is about to complete, Committing event occurs and a message is displayed. 若要使用Installer類別, 您必須在您的專案中參考system.web元件。To use the Installer class, you must reference the System.Configuration.Install assembly in your project.

#using <System.dll>
#using <System.Configuration.Install.dll>

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

// Set 'RunInstaller' attribute to true.

[RunInstaller(true)]
ref class MyInstallerClass: public Installer
{
private:

   // Event handler for 'Committing' event.
   void MyInstaller_Committing( Object^ sender, InstallEventArgs^ e )
   {
      Console::WriteLine( "" );
      Console::WriteLine( "Committing Event occurred." );
      Console::WriteLine( "" );
   }


   // Event handler for 'Committed' event.
   void MyInstaller_Committed( Object^ sender, InstallEventArgs^ e )
   {
      Console::WriteLine( "" );
      Console::WriteLine( "Committed Event occurred." );
      Console::WriteLine( "" );
   }


public:
   MyInstallerClass()
   {
      
      // Attach the 'Committed' event.
      this->Committed += gcnew InstallEventHandler( this, &MyInstallerClass::MyInstaller_Committed );
      
      // Attach the 'Committing' event.
      this->Committing += gcnew InstallEventHandler( this, &MyInstallerClass::MyInstaller_Committing );
   }


   // Override the 'Install' method.
   virtual void Install( IDictionary^ savedState ) override
   {
      Installer::Install( savedState );
   }


   // Override the 'Commit' method.
   virtual void Commit( IDictionary^ savedState ) override
   {
      Installer::Commit( savedState );
   }


   // Override the 'Rollback' method.
   virtual void Rollback( IDictionary^ savedState ) override
   {
      Installer::Rollback( savedState );
   }

};

int main()
{
   Console::WriteLine( "Usage : installutil.exe Installer.exe " );
}

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;

// Set 'RunInstaller' attribute to true.
[RunInstaller(true)]
public class MyInstallerClass: Installer
{
   public MyInstallerClass() :base()
   {
      // Attach the 'Committed' event.
      this.Committed += new InstallEventHandler(MyInstaller_Committed);
      // Attach the 'Committing' event.
      this.Committing += new InstallEventHandler(MyInstaller_Committing);

   }
   // Event handler for 'Committing' event.
   private void MyInstaller_Committing(object sender, InstallEventArgs e)
   {
      Console.WriteLine("");
      Console.WriteLine("Committing Event occurred.");
      Console.WriteLine("");
   }
   // Event handler for 'Committed' event.
   private void MyInstaller_Committed(object sender, InstallEventArgs e)
   {
      Console.WriteLine("");
      Console.WriteLine("Committed Event occurred.");
      Console.WriteLine("");
   }
   // Override the 'Install' method.
   public override void Install(IDictionary savedState)
   {
      base.Install(savedState);
   }
   // Override the 'Commit' method.
   public override void Commit(IDictionary savedState)
   {
      base.Commit(savedState);
   }
   // Override the 'Rollback' method.
   public override void Rollback(IDictionary savedState)
   {
      base.Rollback(savedState);
   }
   public static void Main()
   {
      Console.WriteLine("Usage : installutil.exe Installer.exe ");
   }
}
Imports System.Collections
Imports System.ComponentModel
Imports System.Configuration.Install

' Set 'RunInstaller' attribute to true.
<RunInstaller(True)> _
Public Class MyInstallerClass
   Inherits Installer

   Public Sub New()
       MyBase.New()
      ' Attach the 'Committed' event.
      AddHandler Me.Committed, AddressOf MyInstaller_Committed
      ' Attach the 'Committing' event.
      AddHandler Me.Committing, AddressOf MyInstaller_Committing
   End Sub

   ' Event handler for 'Committing' event.
   Private Sub MyInstaller_Committing(ByVal sender As Object, _
                                      ByVal e As InstallEventArgs)
      Console.WriteLine("")
      Console.WriteLine("Committing Event occurred.")
      Console.WriteLine("")
   End Sub

   ' Event handler for 'Committed' event.
   Private Sub MyInstaller_Committed(ByVal sender As Object, _
                                     ByVal e As InstallEventArgs)
      Console.WriteLine("")
      Console.WriteLine("Committed Event occurred.")
      Console.WriteLine("")
   End Sub

   ' Override the 'Install' method.
   Public Overrides Sub Install(ByVal savedState As IDictionary)
      MyBase.Install(savedState)
   End Sub

   ' Override the 'Commit' method.
   Public Overrides Sub Commit(ByVal savedState As IDictionary)
      MyBase.Commit(savedState)
   End Sub

   ' Override the 'Rollback' method.
   Public Overrides Sub Rollback(ByVal savedState As IDictionary)
      MyBase.Rollback(savedState)
   End Sub
   Public Shared Sub Main()
      Console.WriteLine("Usage : installutil.exe Installer.exe ")
   End Sub
End Class

備註

這是 .NET Framework 中所有自訂安裝程式的基類。This is the base class for all custom installers in the .NET Framework. 安裝程式是協助在電腦上安裝應用程式的元件。Installers are components that help install applications on a computer.

您必須遵循幾個步驟, 才能使用Installer:There are several steps you must follow to use an Installer:

  • Installer繼承類別。Inherit the Installer class.

  • 覆寫Commit 、、和Uninstall方法。 Rollback InstallOverride the Install, Commit, Rollback, and Uninstall methods.

  • 將加入RunInstallerAttribute至您的衍生類別, 並將true其設定為。Add the RunInstallerAttribute to your derived class and set it to true.

  • 將您的衍生類別放在元件中, 並安裝應用程式。Put your derived class in the assembly with your application to install.

  • 叫用安裝程式。Invoke the installers. 例如, 使用 Installutil.exe 來叫用安裝程式。For example, use the InstallUtil.exe to invoke the installers.

Installers屬性包含安裝程式的集合。The Installers property contains a collection of installers. 如果的Installer這個實例是安裝程式集合的一部分, 則Parent會將Installer屬性設定為包含集合的實例。If this instance of Installer is part of an installer collection, the Parent property is set to the Installer instance that contains the collection. 如需使用Installers集合的範例, AssemblyInstaller請參閱類別。For an example of the use of the Installers collection, see the AssemblyInstaller class.

類別的Installer InstallersCommitRollbackUninstall方法會經歷儲存在屬性中的安裝程式集合, 並叫用每個安裝程式的對應方法。 InstallThe Install, Commit, Rollback, and Uninstall methods of the Installer class go through the collection of installers stored in the Installers property, and invokes the corresponding method of each installer.

Commit Rollback Uninstall不一定會在同一個Installer實例上呼叫、、和方法。InstallThe Install, Commit, Rollback, and Uninstall methods are not always called on the same Installer instance. 例如, 在安裝Installer和認可應用程式時, 可能會使用一個實例, 然後釋放該實例的參考。For example, one Installer instance might be used while installing and committing an application, and then the reference to that instance is released. 稍後, 卸載應用程式會建立新Installer實例的參考, 這表示Uninstall方法是由不同的Installer實例所呼叫。Later, uninstalling the application creates a reference to a new Installer instance, meaning that the Uninstall method is called by a different instance of Installer. 基於這個理由, 在您的衍生類別中, 請勿將電腦的狀態儲存在安裝程式中。For this reason, in your derived class, do not save the state of a computer in an installer. 相反地, 請IDictionary使用跨呼叫保留並傳入InstallCommitRollbackUninstall方法的。Instead, use an IDictionary that is preserved across calls and passed into your Install, Commit, Rollback, and Uninstall methods.

有兩種情況說明將資訊儲存在省電IDictionary中的需求。Two situations illustrate the need to save information in the state-saver IDictionary. 首先, 假設您的安裝程式設定了登錄機碼。First, suppose that your installer sets a registry key. 它應該會在中IDictionary儲存金鑰的原始值。It should save the key's original value in the IDictionary. 如果已復原安裝, 則可以還原原始值。If the installation is rolled back, the original value can be restored. 第二, 假設安裝程式取代現有的檔案。Second, suppose the installer replaces an existing file. 將現有的檔案儲存在臨時目錄中, 以及檔案新位置IDictionary的位置。Save the existing file in a temporary directory and the location of the new location of the file in the IDictionary. 如果已復原安裝, 則會刪除較新的檔案, 並由原始的暫存位置取代。If the installation is rolled back, the newer file is deleted and replaced by the original from the temporary location.

Installer.Context屬性包含安裝的相關資訊。The Installer.Context property contains information about the installation. 例如, 用於安裝之記錄檔位置的資訊、用Uninstall來儲存方法所需資訊的檔案位置, 以及執行安裝可執行檔時所輸入的命令列。For example, information about the location of the log file for the installation, the location of the file to save information required by the Uninstall method, and the command line that was entered when the installation executable was run.

建構函式

Installer()

初始化 Installer 類別的新執行個體。Initializes a new instance of the Installer class.

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。Gets a value indicating whether the component can raise an event.

(繼承來源 Component)
Container

取得包含 IContainerComponentGets the IContainer that contains the Component.

(繼承來源 Component)
Context

取得或設定有關目前安裝的資訊。Gets or sets information about the current installation.

DesignMode

取得值,指出 Component 目前是否處於設計模式。Gets a value that indicates whether the Component is currently in design mode.

(繼承來源 Component)
Events

取得附加在這個 Component 上的事件處理常式清單。Gets the list of event handlers that are attached to this Component.

(繼承來源 Component)
HelpText

取得安裝程式集合中所有安裝程式的說明文字。Gets the help text for all the installers in the installer collection.

Installers

取得這個安裝程式包含的安裝程式集合。Gets the collection of installers that this installer contains.

Parent

取得或設定安裝程式,含有這個安裝程式所屬的集合。Gets or sets the installer containing the collection that this installer belongs to.

Site

取得或設定 ComponentISiteGets or sets the ISite of the Component.

(繼承來源 Component)

方法

Commit(IDictionary)

當在衍生類別中被覆寫時,完成安裝異動。When overridden in a derived class, completes the install transaction.

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。Releases all resources used by the Component.

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(繼承來源 Component)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。Returns an object that represents a service provided by the Component or by its Container.

(繼承來源 Component)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
Install(IDictionary)

當在衍生類別中被覆寫時,執行安裝。When overridden in a derived class, performs the installation.

MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
OnAfterInstall(IDictionary)

引發 AfterInstall 事件。Raises the AfterInstall event.

OnAfterRollback(IDictionary)

引發 AfterRollback 事件。Raises the AfterRollback event.

OnAfterUninstall(IDictionary)

引發 AfterUninstall 事件。Raises the AfterUninstall event.

OnBeforeInstall(IDictionary)

引發 BeforeInstall 事件。Raises the BeforeInstall event.

OnBeforeRollback(IDictionary)

引發 BeforeRollback 事件。Raises the BeforeRollback event.

OnBeforeUninstall(IDictionary)

引發 BeforeUninstall 事件。Raises the BeforeUninstall event.

OnCommitted(IDictionary)

引發 Committed 事件。Raises the Committed event.

OnCommitting(IDictionary)

引發 Committing 事件。Raises the Committing event.

Rollback(IDictionary)

當在衍生類別中被覆寫時,還原電腦安裝之前的狀態。When overridden in a derived class, restores the pre-installation state of the computer.

ToString()

傳回任何包含 Component 名稱的 StringReturns a String containing the name of the Component, if any. 不應覆寫此方法。This method should not be overridden.

(繼承來源 Component)
Uninstall(IDictionary)

當在衍生類別中被覆寫時,移除安裝。When overridden in a derived class, removes an installation.

事件

AfterInstall

發生於 Install(IDictionary) 屬性中的所有安裝程式的 Installers 方法都已經執行之後。Occurs after the Install(IDictionary) methods of all the installers in the Installers property have run.

AfterRollback

發生於 Installers 屬性中所有安裝程式的安裝都復原之後。Occurs after the installations of all the installers in the Installers property are rolled back.

AfterUninstall

發生於 Installers 屬性中的所有安裝程式執行其解除安裝作業之後。Occurs after all the installers in the Installers property perform their uninstallation operations.

BeforeInstall

發生於安裝程式集合中的每個安裝程式的 Install(IDictionary) 方法執行之前。Occurs before the Install(IDictionary) method of each installer in the installer collection has run.

BeforeRollback

發生於 Installers 屬性中的安裝程式復原之前。Occurs before the installers in the Installers property are rolled back.

BeforeUninstall

發生於 Installers 屬性中的安裝程式執行其解除安裝作業之前。Occurs before the installers in the Installers property perform their uninstall operations.

Committed

發生於 Installers 屬性中的所有安裝程式都認可其安裝之後。Occurs after all the installers in the Installers property have committed their installations.

Committing

發生於 Installers 屬性中的安裝程式認可其安裝之前。Occurs before the installers in the Installers property commit their installations.

Disposed

當此元件由 Dispose() 方法的呼叫處置時發生。Occurs when the component is disposed by a call to the Dispose() method.

(繼承來源 Component)

適用於

另請參閱