Installer 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供自定义安装的基础。
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. 的类。 何时 Commit 即将完成, Committing 将发生事件并显示消息。 若要使用该 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:
Installer继承类。
将它 RunInstallerAttribute 添加到派生类,并将其设置为
true
.使用应用程序将派生类放入程序集中以安装。
调用安装程序。 例如,使用InstallUtil.exe调用安装程序。
该 Installers 属性包含安装程序的集合。 如果此实例 Installer 是安装程序集合的一部分,该 Parent 属性将设置为 Installer 包含集合的实例。 有关使用集合的示例 Installers ,请参阅 AssemblyInstaller 类。
Install类的 Installer 、Commit、Rollback方法和Uninstall方法将遍历存储在属性中的Installers安装程序的集合,并调用每个安装程序的相应方法。
不Install一定始终在同一Installer实例上调用和CommitRollbackUninstall方法。 例如,在安装和提交应用程序时可以使用一个 Installer 实例,然后释放对该实例的引用。 稍后,卸载应用程序会创建对新 Installer 实例的引用,这意味着该方法 Uninstall 是由另一个实例调用的 Installer。 因此,在派生类中,不要在安装程序中保存计算机的状态。 请改用在调用中保留并传递到你的Install、Commit和RollbackUninstall方法中的一个IDictionary。
两种情况说明了需要在状态保存器 IDictionary中保存信息。 首先,假设安装程序设置了注册表项。 It should save the key's original value in the IDictionary. 如果回滚安装,可以还原原始值。 其次,假设安装程序将替换现有文件。 将现有文件保存在临时目录中,以及文件的新位置的位置 IDictionary。 如果回滚安装,则会删除较新的文件,并将其替换为临时位置中的原始文件。
该 Installer.Context 属性包含有关安装的信息。 例如,有关安装日志文件的位置、用于保存方法所需的 Uninstall 信息的文件的位置以及运行安装可执行文件时输入的命令行的信息的信息。
构造函数
Installer() |
初始化 Installer 类的新实例。 |
属性
CanRaiseEvents |
获取一个指示组件是否可以引发事件的值。 (继承自 Component) |
Container |
获取包含 IContainer 的 Component。 (继承自 Component) |
Context |
获取或设置关于当前安装的信息。 |
DesignMode |
获取一个值,用以指示 Component 当前是否处于设计模式。 (继承自 Component) |
Events |
获取附加到此 Component 的事件处理程序的列表。 (继承自 Component) |
HelpText |
获取安装程序集合中所有安装程序的帮助文字。 |
Installers |
获取该安装程序包含的安装程序的集合。 |
Parent |
获取或设置包含该安装程序所属的集合的安装程序。 |
Site | (继承自 Component) |
方法
事件
AfterInstall |
在 Installers 属性中的所有安装程序的 Install(IDictionary) 方法都运行后发生。 |
AfterRollback |
在回滚 Installers 属性中所有安装程序的安装后发生。 |
AfterUninstall |
在 Installers 属性中所有安装程序都执行它们的卸载操作后发生。 |
BeforeInstall |
在安装程序集合中每个安装程序的 Install(IDictionary) 方法运行前发生。 |
BeforeRollback |
在回滚 Installers 属性中的安装程序前发生。 |
BeforeUninstall |
在 Installers 属性中的安装程序执行它们的卸载操作前发生。 |
Committed |
在 Installers 属性中的所有安装程序均提交它们的安装后发生。 |
Committing |
在 Installers 属性中的安装程序提交它们的安装前发生。 |
Disposed |
在通过调用 Dispose() 方法释放组件时发生。 (继承自 Component) |