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 、 RollbackCommit和 Uninstall 方法會經歷儲存在 屬性中的Installers安裝程式集合,並叫用每個安裝程式的對應方法。
Install相同實例上Installer不一定呼叫、 CommitRollback和 Uninstall 方法。 例如,在安裝和認可應用程式時,可能會使用一個 Installer 實例,然後釋放該實例的參考。 稍後,卸載應用程式會建立新Installer實例的Installer參考,這表示 Uninstall 方法是由 不同的 實例呼叫。 基於這個理由,在您的衍生類別中,請勿在安裝程式中儲存計算機的狀態。 請改用 IDictionary 跨呼叫保留的,並傳遞至您的 Install、 Commit、 Rollback和 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 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應