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实例上调用 、Commit、 Rollback和 Uninstall 方法。 例如, Installer 安装和提交应用程序时可能会使用一个实例,然后释放对该实例的引用。 稍后,卸载应用程序会创建对新 Installer 实例的引用,这意味着 Uninstall 该方法由 的其他实例 Installer调用。 因此,在派生类中,不要在安装程序中保存计算机的状态。 请改用IDictionary在调用中保留并传递到 Install、、 CommitRollback和 Uninstall 方法的 。
两种情况表明需要在状态保护器 IDictionary中保存信息。 首先,假设安装程序设置了注册表项。 它应将密钥的原始值保存在 中 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) |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈