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. 它创建一个继承自的类 InstallerIt 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.Configu。 在项目中安装程序集。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.

若要使用,必须执行以下几个步骤 InstallerThere are several steps you must follow to use an Installer:

  • 继承 Installer 类。Inherit the Installer class.

  • 重写 InstallCommitRollbackUninstall 方法。Override the Install, Commit, Rollback, and Uninstall methods.

  • 将添加 RunInstallerAttribute 到派生类中,并将其设置为 trueAdd 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.

Install类的、、 Commit RollbackUninstall 方法 Installer 通过存储在属性中的安装程序的集合, Installers 并调用每个安装程序的相应方法。The 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.

Install Commit Rollback Uninstall 不总是对同一实例调用、、和方法 InstallerThe 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 方法由不同的实例调用 InstallerLater, 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.

两种情况阐释了在状态保护中保存信息的需要 IDictionaryTwo situations illustrate the need to save information in the state-saver IDictionary. 首先,假设安装程序设置了注册表项。First, suppose that your installer sets a registry key. 它应在中保存密钥的原始值 IDictionaryIt 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. 将现有文件保存在临时目录中,并将该文件的新位置的位置保存在中 IDictionarySave 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)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。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 占用的非托管资源,还可以另外再释放托管资源。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()

检索控制此实例的生存期策略的当前生存期服务对象。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 的浅表副本。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 的名称的 String(如果有)。Returns 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

Installers 属性中的所有安装程序的 Install(IDictionary) 方法都运行后发生。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)

适用于

另请参阅