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
- 继承
示例
以下示例演示 类的 TransactedInstallerTransactedInstaller 、 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) |