Installer 類別

定義

提供自訂安裝的基礎。

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

範例

下列範例示範 類別的使用 Installer 。 它會建立繼承自 Installer 的類別。 當 即將完成時 CommitCommitting 就會發生事件,並顯示訊息。 若要使用 類別 Installer ,您必須參考專案中的 System.Configuration.Install 元件。

#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中所有自訂安裝程式的基類。 安裝程式是協助在電腦上安裝應用程式的元件。

您必須遵循幾個步驟,才能使用 Installer

  • Installer繼承 類別。

  • 覆寫 InstallCommitRollbackUninstall 方法。

  • RunInstallerAttribute將 新增至衍生類別,並將其設定為 true

  • 將衍生類別放在要安裝的應用程式元件中。

  • 叫用安裝程式。 例如,使用 InstallUtil.exe 來叫用安裝程式。

屬性 Installers 包含安裝程式的集合。 如果 的這個實例 Installer 是安裝程式集合的一部分,屬性 Parent 就會設定為 Installer 包含集合的實例。 如需使用集合的 Installers 範例,請參閱 類別 AssemblyInstaller

類別 InstallInstallerRollbackCommitUninstall 方法會經歷儲存在 屬性中的 Installers 安裝程式集合,並叫用每個安裝程式的對應方法。

InstallCommitRollbackUninstall 方法不一定會在相同的 Installer 實例上呼叫。 例如,在安裝和認可應用程式時,可能會使用一個 Installer 實例,然後釋放該實例的參考。 稍後,卸載應用程式會建立新 Installer 實例的參考,這表示 Uninstall 方法是由不同的 實例 Installer 呼叫。 因此,在您的衍生類別中,請勿在安裝程式中儲存電腦的狀態。 請改用 IDictionary 在呼叫之間保留的 ,並傳遞至您的 InstallCommitRollbackUninstall 方法。

兩種情況說明在狀態儲存器 IDictionary 中儲存資訊的需求。 首先,假設您的安裝程式設定登錄機碼。 它應該會將金鑰的原始值儲存在 中 IDictionary 。 如果復原安裝,則可以還原原始值。 其次,假設安裝程式取代了現有的檔案。 將現有的檔案儲存在臨時目錄中,以及 檔案新位置的位置。 IDictionary 如果復原安裝,則會從暫存位置刪除並取代較新的檔案。

屬性 Installer.Context 包含安裝的相關資訊。 例如,安裝之記錄檔位置的相關資訊、儲存 方法所需 Uninstall 資訊的檔案位置,以及執行安裝可執行檔時所輸入的命令列。

建構函式

Installer()

初始化 Installer 類別的新執行個體。

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
Container

取得包含 IContainerComponent

(繼承來源 Component)
Context

取得或設定有關目前安裝的資訊。

DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
HelpText

取得安裝程式集合中所有安裝程式的說明文字。

Installers

取得這個安裝程式包含的安裝程式集合。

Parent

取得或設定安裝程式,含有這個安裝程式所屬的集合。

Site

取得或設定 ComponentISite

(繼承來源 Component)

方法

Commit(IDictionary)

當在衍生類別中被覆寫時,完成安裝異動。

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 Component)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
Install(IDictionary)

當在衍生類別中被覆寫時,執行安裝。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
OnAfterInstall(IDictionary)

引發 AfterInstall 事件。

OnAfterRollback(IDictionary)

引發 AfterRollback 事件。

OnAfterUninstall(IDictionary)

引發 AfterUninstall 事件。

OnBeforeInstall(IDictionary)

引發 BeforeInstall 事件。

OnBeforeRollback(IDictionary)

引發 BeforeRollback 事件。

OnBeforeUninstall(IDictionary)

引發 BeforeUninstall 事件。

OnCommitted(IDictionary)

引發 Committed 事件。

OnCommitting(IDictionary)

引發 Committing 事件。

Rollback(IDictionary)

當在衍生類別中被覆寫時,還原電腦安裝之前的狀態。

ToString()

傳回任何包含 Component 名稱的 String。 不應覆寫此方法。

(繼承來源 Component)
Uninstall(IDictionary)

當在衍生類別中被覆寫時,移除安裝。

事件

AfterInstall

發生於 Installers 屬性中所有安裝程式的 Install(IDictionary) 方法都執行之後。

AfterRollback

發生於 Installers 屬性中所有安裝程式的安裝都復原之後。

AfterUninstall

發生於 Installers 屬性中的所有安裝程式執行其解除安裝作業之後。

BeforeInstall

發生於安裝程式集合中每個安裝程式的 Install(IDictionary) 方法執行之前。

BeforeRollback

發生於 Installers 屬性中的安裝程式復原之前。

BeforeUninstall

發生於 Installers 屬性中的安裝程式執行其解除安裝作業之前。

Committed

發生於 Installers 屬性中的所有安裝程式都認可其安裝之後。

Committing

發生於 Installers 屬性中的安裝程式認可其安裝之前。

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)

適用於

另請參閱