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必须遵循几个步骤:

属性 Installers 包含安装程序的集合。 如果 的 Installer 此实例是安装程序集合的一部分,则 Parent 属性设置为 Installer 包含该集合的 实例。 有关集合用法 Installers 的示例,请参阅 AssemblyInstaller 类。

Install类的 InstallerCommitRollbackUninstall 方法将遍历 存储在 属性中的Installers安装程序集合,并调用每个安装程序的相应方法。

Install总是在同一Installer实例上调用 、CommitRollbackUninstall 方法。 例如, Installer 安装和提交应用程序时可能会使用一个实例,然后释放对该实例的引用。 稍后,卸载应用程序会创建对新 Installer 实例的引用,这意味着 Uninstall 该方法由 的其他实例 Installer调用。 因此,在派生类中,不要在安装程序中保存计算机的状态。 请改用IDictionary在调用中保留并传递到 Install、、 CommitRollbackUninstall 方法的 。

两种情况表明需要在状态保护器 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)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(继承自 MarshalByRefObject)
Dispose()

释放由 Component 使用的所有资源。

(继承自 Component)
Dispose(Boolean)

释放由 Component 占用的非托管资源,还可以另外再释放托管资源。

(继承自 Component)
Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 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)

适用于

另请参阅