Installer クラス

カスタム インストールに利用できる基本機能を提供します。

この型のすべてのメンバの一覧については、Installer メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Configuration.Install.Installer
            System.Configuration.Install.AssemblyInstaller
            System.Configuration.Install.ComponentInstaller
            System.Configuration.Install.TransactedInstaller
            System.Management.Instrumentation.DefaultManagementProjectInstaller
            System.Management.Instrumentation.ManagementInstaller

Public Class Installer
   Inherits Component
[C#]
public class Installer : Component
[C++]
public __gc class Installer : public Component
[JScript]
public class Installer extends Component

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

このクラスは、.NET Framework におけるカスタム インストーラすべての基本クラスです。インストーラは、コンピュータへのアプリケーションのインストールを支援するコンポーネントです。

Installer を使用するには、以下の手順を実行する必要があります。

  • Installer クラスから継承します。
  • InstallCommitRollbackUninstall の各メソッドをオーバーライドします。
  • RunInstallerAttribute を派生クラスに追加し、その値を true に設定します。
  • 派生クラスおよびインストールするアプリケーションをアセンブリに配置します。
  • インストーラを起動します。たとえば、InstallUtil.exe を使用してインストーラを起動します。

Installers プロパティは、インストーラのコレクションを格納します。 Installer のインスタンスがインストーラ コレクションの一部である場合、 Parent プロパティに、そのコレクションを格納する Installer インスタンスが設定されます。 Installers コレクションの使用例については、 AssemblyInstaller クラスのトピックを参照してください。

Installer クラスの InstallCommitRollbackUninstall の各メソッドは、 Installers プロパティに格納されているコレクション内のインストーラに順次アクセスし、各インストーラに対応するメソッドを呼び出します。

InstallCommitRollbackUninstall の各メソッドは、同じ Installer インスタンスに対して呼び出されるとは限りません。たとえば、 Installer インスタンスを使用してアプリケーションをインストールしてコミットした後で、そのインスタンスへの参照は解放します。後からそのアプリケーションをアンインストールするときには、新しい Installer インスタンスへの参照が作成されます。つまり、 Uninstall メソッドは、 Installer の別のインスタンスに対して呼び出されることになります。このため、派生クラスでは、コンピュータの状態をインストーラには保存しないでください。代わりに、 IDictionary を使用してください。ディクショナリに保存した情報は、各種の呼び出しにわたって維持され、 InstallCommitRollbackUninstall の各メソッドに渡されます。

状態を保存する IDictionary に情報を保存する必要がある 2 つの状況を次に示します。まず、インストーラが 1 つのレジストリ キーを設定するとします。この場合、キーの元の値を IDictionary に保存します。これにより、インストールがロールバックされた場合に元の値を復元できます。2 つ目の状況として、インストーラが既存のファイルを置換すると想定します。既存のファイルを一時ディレクトリに保存し、そのファイルの新しい位置を IDictionary に保存します。これにより、インストールがロールバックされた場合は、新しいファイルを削除して、一時ディレクトリ内の元のファイルで置換できます。

Installer.Context プロパティは、インストールに関する情報を格納します。たとえば、インストールに関するログ ファイルの位置、 Uninstall メソッドが必要とする情報を保存するファイルの位置、インストール実行可能ファイルが実行されたときに入力されたコマンド ラインなどの情報が格納されます。

使用例

[Visual Basic, C#, C++] Installer クラスを使用する例を次に示します。この例では、 Installer から継承するクラスを作成します。 Commit が完了するときに Committing イベントが発生し、メッセージが表示されます。

 
Imports System
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 'New

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

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

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

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

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

[C#] 
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 occured.");
      Console.WriteLine("");
   }
   // Event handler for 'Committed' event.
   private void MyInstaller_Committed(object sender, InstallEventArgs e)
   {
      Console.WriteLine("");
      Console.WriteLine("Committed Event occured.");
      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 ");
   }
}

[C++] 
#using <mscorlib.dll>
#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)]
__gc class MyInstallerClass : public Installer {
    // Event handler for 'Committing' event.
private:
    void MyInstaller_Committing(Object* sender, InstallEventArgs* e) {
        Console::WriteLine(S"");
        Console::WriteLine(S"Committing Event occured.");
        Console::WriteLine(S"");
    }
    // Event handler for 'Committed' event.
    void MyInstaller_Committed(Object* sender, InstallEventArgs* e) {
        Console::WriteLine(S"");
        Console::WriteLine(S"Committed Event occured.");
        Console::WriteLine(S"");
    }

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

    }

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

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

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

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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Configuration.Install

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Configuration.Install (System.Configuration.Install.dll 内)

参照

Installer メンバ | System.Configuration.Install 名前空間 | AssemblyInstaller | ComponentInstaller | InstallerCollection | TransactedInstaller | インストーラ ツール (Installutil.exe)