TransactedInstaller 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義完全成功或失敗的安裝程式,並且保留電腦在初始狀態。
public ref class TransactedInstaller : System::Configuration::Install::Installer
public class TransactedInstaller : System.Configuration.Install.Installer
type TransactedInstaller = class
inherit Installer
Public Class TransactedInstaller
Inherits Installer
- 繼承
範例
下列範例示範 TransactedInstaller類別的 TransactedInstaller 、 Install 和 Uninstall 方法。
此範例提供與 Installutil.exe (Installer Tool) 類似的實作。 它會使用該特定元件前面的選項來安裝元件。 如果未為元件指定選項,則如果清單中有先前的元件,則會使用先前元件的選項。 如果指定了 “/u” 或 “/uninstall” 選項,則會卸載元件。 如果提供 “/?” 或 “/help” 選項,則會向控制台顯示說明資訊。
array<String^>^ args = Environment::GetCommandLineArgs();
ArrayList^ myOptions = gcnew ArrayList;
String^ myOption;
bool toUnInstall = false;
bool toPrintHelp = false;
TransactedInstaller^ myTransactedInstaller = gcnew TransactedInstaller;
AssemblyInstaller^ myAssemblyInstaller;
InstallContext^ myInstallContext;
try
{
for ( int i = 1; i < args->Length; i++ )
{
// Process the arguments.
if ( args[ i ]->StartsWith( "/" ) || args[ i ]->StartsWith( "-" ) )
{
myOption = args[ i ]->Substring( 1 );
// Determine whether the option is to 'uninstall' an assembly.
if ( String::Compare( myOption, "u", true ) == 0 ||
String::Compare( myOption, "uninstall", true ) == 0 )
{
toUnInstall = true;
continue;
}
// Determine whether the option is for printing help information.
if ( String::Compare( myOption, "?", true ) == 0 ||
String::Compare( myOption, "help", true ) == 0 )
{
toPrintHelp = true;
continue;
}
// Add the option encountered to the list of all options
// encountered for the current assembly.
myOptions->Add( myOption );
}
else
{
// Determine whether the assembly file exists.
if ( !File::Exists( args[ i ] ) )
{
// If assembly file doesn't exist then print error.
Console::WriteLine( "\nError : {0} - Assembly file doesn't exist.",
args[ i ] );
return 0;
}
// Create a instance of 'AssemblyInstaller' that installs the given assembly.
myAssemblyInstaller =
gcnew AssemblyInstaller( args[ i ],
(array<String^>^)( myOptions->ToArray( String::typeid ) ) );
// Add the instance of 'AssemblyInstaller' to the 'TransactedInstaller'.
myTransactedInstaller->Installers->Add( myAssemblyInstaller );
}
}
// If user requested help or didn't provide any assemblies to install
// then print help message.
if ( toPrintHelp || myTransactedInstaller->Installers->Count == 0 )
{
PrintHelpMessage();
return 0;
}
// Create a instance of 'InstallContext' with the options specified.
myInstallContext =
gcnew InstallContext( "Install.log",
(array<String^>^)( myOptions->ToArray( String::typeid ) ) );
myTransactedInstaller->Context = myInstallContext;
// Install or Uninstall an assembly depending on the option provided.
if ( !toUnInstall )
{
myTransactedInstaller->Install( gcnew Hashtable );
}
else
{
myTransactedInstaller->Uninstall( nullptr );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "\nException raised : {0}", e->Message );
}
using System;
using System.ComponentModel;
using System.Collections;
using System.Configuration.Install;
using System.IO;
public class TransactedInstaller_Example
{
public static void Main(String[] args)
{
ArrayList myOptions = new ArrayList();
String myOption;
bool toUnInstall = false;
bool toPrintHelp = false;
TransactedInstaller myTransactedInstaller = new TransactedInstaller();
AssemblyInstaller myAssemblyInstaller;
InstallContext myInstallContext;
try
{
for(int i = 0; i < args.Length; i++)
{
// Process the arguments.
if(args[i].StartsWith("/") || args[i].StartsWith("-"))
{
myOption = args[i].Substring(1);
// Determine whether the option is to 'uninstall' an assembly.
if(String.Compare(myOption, "u", true) == 0 ||
String.Compare(myOption, "uninstall", true) == 0)
{
toUnInstall = true;
continue;
}
// Determine whether the option is for printing help information.
if(String.Compare(myOption, "?", true) == 0 ||
String.Compare(myOption, "help", true) == 0)
{
toPrintHelp = true;
continue;
}
// Add the option encountered to the list of all options
// encountered for the current assembly.
myOptions.Add(myOption);
}
else
{
// Determine whether the assembly file exists.
if(!File.Exists(args[i]))
{
// If assembly file doesn't exist then print error.
Console.WriteLine("\nError : {0} - Assembly file doesn't exist.",
args[i]);
return;
}
// Create a instance of 'AssemblyInstaller' that installs the given assembly.
myAssemblyInstaller =
new AssemblyInstaller(args[i],
(string[]) myOptions.ToArray(typeof(string)));
// Add the instance of 'AssemblyInstaller' to the 'TransactedInstaller'.
myTransactedInstaller.Installers.Add(myAssemblyInstaller);
}
}
// If user requested help or didn't provide any assemblies to install
// then print help message.
if(toPrintHelp || myTransactedInstaller.Installers.Count == 0)
{
PrintHelpMessage();
return;
}
// Create a instance of 'InstallContext' with the options specified.
myInstallContext =
new InstallContext("Install.log",
(string[]) myOptions.ToArray(typeof(string)));
myTransactedInstaller.Context = myInstallContext;
// Install or Uninstall an assembly depending on the option provided.
if(!toUnInstall)
myTransactedInstaller.Install(new Hashtable());
else
myTransactedInstaller.Uninstall(null);
}
catch(Exception e)
{
Console.WriteLine("\nException raised : {0}", e.Message);
}
}
public static void PrintHelpMessage()
{
Console.WriteLine("Usage : TransactedInstaller [/u | /uninstall] [option [...]] assembly" +
"[[option [...]] assembly] [...]]");
Console.WriteLine("TransactedInstaller executes the installers in each of" +
" the given assembly. If /u or /uninstall option" +
" is given it uninstalls the assemblies.");
}
}
Dim options As New ArrayList()
Dim myOption As String
Dim toUnInstall As Boolean = False
Dim toPrintHelp As Boolean = False
Dim myTransactedInstaller As New TransactedInstaller()
Dim myAssemblyInstaller As AssemblyInstaller
Dim myInstallContext As InstallContext
Try
Dim i As Integer
For i = 1 To args.Length - 1
' Process the arguments.
If args(i).StartsWith("/") Or args(i).StartsWith("-") Then
myOption = args(i).Substring(1)
' Determine whether the option is to 'uninstall' an assembly.
If String.Compare(myOption, "u", True) = 0 Or _
String.Compare(myOption,"uninstall", True) = 0 Then
toUnInstall = True
GoTo ContinueFor1
End If
' Determine whether the option is for printing help information.
If String.Compare(myOption, "?", True) = 0 Or _
String.Compare(myOption, "help", True) = 0 Then
toPrintHelp = True
GoTo ContinueFor1
End If
' Add the option encountered to the list of all options
' encountered for the current assembly.
options.Add(myOption)
Else
' Determine whether the assembly file exists.
If Not File.Exists(args(i)) Then
' If assembly file doesn't exist then print error.
Console.WriteLine(ControlChars.Newline + _
"Error : {0} - Assembly file doesn't exist.", args(i))
Return
End If
' Create a instance of 'AssemblyInstaller' that installs the given assembly.
myAssemblyInstaller = New AssemblyInstaller(args(i), _
CType(options.ToArray(GetType(String)), String()))
' Add the instance of 'AssemblyInstaller' to the 'TransactedInstaller'.
myTransactedInstaller.Installers.Add(myAssemblyInstaller)
End If
ContinueFor1:
Next i
' If user requested help or didn't provide any assemblies to install
' then print help message.
If toPrintHelp Or myTransactedInstaller.Installers.Count = 0 Then
PrintHelpMessage()
Return
End If
' Create a instance of 'InstallContext' with the options specified.
myInstallContext = New InstallContext("Install.log", _
CType(options.ToArray(GetType(String)), String()))
myTransactedInstaller.Context = myInstallContext
' Install or Uninstall an assembly depending on the option provided.
If Not toUnInstall Then
myTransactedInstaller.Install(New Hashtable())
Else
myTransactedInstaller.Uninstall(Nothing)
End If
Catch e As Exception
Console.WriteLine(ControlChars.Newline + "Exception raised : {0}", e.Message)
End Try
備註
若要在交易中執行安裝程式,請將它們新增至 Installers 這個 TransactedInstaller 實例的屬性。
建構函式
TransactedInstaller() |
初始化 TransactedInstaller 類別的新執行個體。 |
屬性
CanRaiseEvents |
取得值,指出元件是否能引發事件。 (繼承來源 Component) |
Container |
取得包含 IContainer 的 Component。 (繼承來源 Component) |
Context |
取得或設定有關目前安裝的資訊。 (繼承來源 Installer) |
DesignMode |
取得值,指出 Component 目前是否處於設計模式。 (繼承來源 Component) |
Events |
取得附加在這個 Component 上的事件處理常式清單。 (繼承來源 Component) |
HelpText |
取得安裝程式集合中所有安裝程式的說明文字。 (繼承來源 Installer) |
Installers |
取得這個安裝程式包含的安裝程式集合。 (繼承來源 Installer) |
Parent |
取得或設定安裝程式,含有這個安裝程式所屬的集合。 (繼承來源 Installer) |
Site | (繼承來源 Component) |
方法
事件
AfterInstall |
發生於 Installers 屬性中所有安裝程式的 Install(IDictionary) 方法都執行之後。 (繼承來源 Installer) |
AfterRollback |
發生於 Installers 屬性中所有安裝程式的安裝都復原之後。 (繼承來源 Installer) |
AfterUninstall |
發生於 Installers 屬性中的所有安裝程式執行其解除安裝作業之後。 (繼承來源 Installer) |
BeforeInstall |
發生於安裝程式集合中每個安裝程式的 Install(IDictionary) 方法執行之前。 (繼承來源 Installer) |
BeforeRollback |
發生於 Installers 屬性中的安裝程式復原之前。 (繼承來源 Installer) |
BeforeUninstall |
發生於 Installers 屬性中的安裝程式執行其解除安裝作業之前。 (繼承來源 Installer) |
Committed |
發生於 Installers 屬性中的所有安裝程式都認可其安裝之後。 (繼承來源 Installer) |
Committing |
發生於 Installers 屬性中的安裝程式認可其安裝之前。 (繼承來源 Installer) |
Disposed |
當 Dispose() 方法的呼叫處置元件時,就會發生。 (繼承來源 Component) |