Installer自定義安裝類

這是 .NET Framework 中所有自定義安裝程序的基類。安裝程序是幫助在計算機上安裝應用程序的組件。

使用 Installer 時必須遵循幾個步驟。

  • 繼承 Installer 類。

  • 重寫 InstallCommitRollbackUninstall 方法。

  • 向派生類添加 RunInstallerAttribute,並將其設置爲 true

  • 將派生類放置在帶有要安裝的應用程序的程序集內。

  • 調用安裝程序。例如,使用 InstallUtil.exe 調用安裝程序。

Installers 屬性包含安裝程序的集合。如果 Installer 的此實例是安裝程序集合的一部分,則 Parent 屬性設置爲包含該集合的 Installer 實例。有關 Installers 集合用法的示例,請參見 AssemblyInstaller 類。

Installer 類的 InstallCommitRollbackUninstall 方法遍歷存儲在 Installers 屬性中的安裝程序的集合,並調用每個安裝程序的相應方法。

InstallCommitRollbackUninstall 方法並非總是在同一 Installer 實例上調用。例如,在安裝和提交應用程序時可能使用一個 Installer 實例,然後釋放對該實例的引用。以後卸載應用程序時將創建對新的 Installer 實例的引用,這意味着由 Installer 的另一個實例調用 Uninstall 方法。因此,在派生類中,不要在安裝程序中保存計算機的狀態。而請使用 IDictionary,它可跨調用保留並傳遞給 InstallCommitRollbackUninstall 方法。

兩種情況可以闡釋在狀態保護程序 IDictionary 中保存信息的必要性。第一種情況,假定安裝程序設置了註冊表項。它應在 IDictionary 中保存該項的原始值。如果安裝被回滾,則可以還原原始值。第二種情況,假定安裝程序替換現有文件。將現有文件保存在臨時目錄中,並將該文件的新位置的位置保存在 IDictionary 中。如果安裝被回滾,則刪除新文件並將其替換爲臨時位置中的原始文件。

Installer.Context 屬性包含關於安裝的信息。例如,關於安裝日誌文件位置的信息,Uninstall 方法所要求的保存文件的位置的信息,以及運行安裝可執行文件時輸入的命令行。

示例

下面的示例說明如何使用 Installer 類。它創建從 Installer 繼承的類。當 Commit 將要完成時,發生 Committing 事件並顯示一則消息。

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 ");
   }
}

import System.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Configuration.Install.*;

// Set 'RunInstaller' attribute to true.
/** @attribute RunInstaller(true)
 */
public class MyInstallerClass extends Installer
{
    public MyInstallerClass()
    {
        // Attach the 'Committed' event.
        this.add_Committed(new InstallEventHandler(MyInstaller_Committed));

        // Attach the 'Committing' event.
        this.add_Committing(new InstallEventHandler(MyInstaller_Committing));
    } //MyInstallerClass

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

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

    // Override the 'Install' method.
    public void Install(IDictionary savedState)
    {
        super.Install(savedState);
    } //Install

    // Override the 'Commit' method.
    public void Commit(IDictionary savedState)
    {
        super.Commit(savedState);
    } //Commit

    // Override the 'Rollback' method.
    public void Rollback(IDictionary savedState)
    {
        super.Rollback(savedState);
    } //Rollback

    public static void main(String[] args)
    {
        Console.WriteLine("Usage : installutil.exe Installer.exe ");
    } //main
} //MyInstallerClass

線程安全

此類型的任何公共靜態(Visual Basic 中的 Shared)成員都是線程安全的,但不保證所有實例成員都是線程安全的。
平臺

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

 

Windows Vista、Microsoft Windows XP SP2 和 Windows Server 2003 SP1 支持 Microsoft .NET Framework 3.0。

版本信息

.NET Framework

受以下版本支持:3.0、2.0、1.1、1.0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章