Installer 类

提供自定义安装的基础。

**命名空间:**System.Configuration.Install
**程序集:**System.Configuration.Install(在 system.configuration.install.dll 中)

语法

声明
Public Class Installer
    Inherits Component
用法
Dim instance As Installer
public class Installer : Component
public ref class Installer : public Component
public class Installer extends Component
public class Installer extends Component

备注

这是 .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 事件并显示一则消息。

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
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 ");
   }
}
#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 occured." );
      Console::WriteLine( "" );
   }


   // Event handler for 'Committed' event.
   void MyInstaller_Committed( Object^ sender, InstallEventArgs^ e )
   {
      Console::WriteLine( "" );
      Console::WriteLine( "Committed Event occured." );
      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 " );
}
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

继承层次结构

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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Installer 成员
System.Configuration.Install 命名空间
AssemblyInstaller 类
ComponentInstaller 类
InstallerCollection
TransactedInstaller

其他资源

安装程序工具 (Installutil.exe)