MSDN.WhiteKnight - Stack Overflow answers
Ответ на "C# WPF Отображение на форме хода установки msi установщика"
Answer 989398
Можно. Для этого необходимо использовать Windows Installer API. Например, так можно вывести в TextBox действия и возникающие ошибки в ходе установки:
using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Runtime.InteropServices; using System.Threading.Tasks; namespace WpfApp1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } const string msipath = "C:\\Distr\\1.msi"; public int MyInstallUIHandler(IntPtr pvContext, uint iMessageType, string szMessage) { this.Dispatcher.Invoke(() => { textBox1.Text += szMessage + Environment.NewLine; }); return 1; } public void InstallMsi(string msipath, bool uninstall) { string pars = ""; if (uninstall) pars = "REMOVE=ALL"; IntPtr ptr = IntPtr.Zero; InstallUIHandler handler = new InstallUIHandler(MyInstallUIHandler); //отключаем стандартный UI MsiSetInternalUI(INSTALLUILEVEL.INSTALLUILEVEL_NONE, ref ptr); //устанавливаем обработчик сообщений MsiSetExternalUI( handler, INSTALLLOGMODE.INSTALLLOGMODE_ACTIONSTART | INSTALLLOGMODE.INSTALLLOGMODE_ERROR, IntPtr.Zero ); //запускаем установку (удаление) MsiInstallProduct(msipath, pars); } private void Button_Click(object sender, RoutedEventArgs e) { Task.Run(() => { InstallMsi(msipath, false); }); } enum INSTALLMESSAGE { INSTALLMESSAGE_FATALEXIT = 0x00000000, // premature termination, possibly fatal OOM INSTALLMESSAGE_ERROR = 0x01000000, // formatted error message INSTALLMESSAGE_WARNING = 0x02000000, // formatted warning message INSTALLMESSAGE_USER = 0x03000000, // user request message INSTALLMESSAGE_INFO = 0x04000000, // informative message for log INSTALLMESSAGE_FILESINUSE = 0x05000000, // list of files in use that need to be replaced INSTALLMESSAGE_RESOLVESOURCE = 0x06000000, // request to determine a valid source location INSTALLMESSAGE_OUTOFDISKSPACE = 0x07000000, // insufficient disk space message INSTALLMESSAGE_ACTIONSTART = 0x08000000, // start of action: action name & description INSTALLMESSAGE_ACTIONDATA = 0x09000000, // formatted data associated with individual action item INSTALLMESSAGE_PROGRESS = 0x0A000000, // progress gauge info: units so far, total INSTALLMESSAGE_COMMONDATA = 0x0B000000, // product info for dialog: language Id, dialog caption INSTALLMESSAGE_INITIALIZE = 0x0C000000, // sent prior to UI initialization, no string data INSTALLMESSAGE_TERMINATE = 0x0D000000, // sent after UI termination, no string data INSTALLMESSAGE_SHOWDIALOG = 0x0E000000 // sent prior to display or authored dialog or wizard } enum INSTALLLOGMODE // bit flags for use with MsiEnableLog and MsiSetExternalUI { INSTALLLOGMODE_FATALEXIT = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_FATALEXIT >> 24)), INSTALLLOGMODE_ERROR = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_ERROR >> 24)), INSTALLLOGMODE_WARNING = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_WARNING >> 24)), INSTALLLOGMODE_USER = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_USER >> 24)), INSTALLLOGMODE_INFO = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_INFO >> 24)), INSTALLLOGMODE_RESOLVESOURCE = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_RESOLVESOURCE >> 24)), INSTALLLOGMODE_OUTOFDISKSPACE = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_OUTOFDISKSPACE >> 24)), INSTALLLOGMODE_ACTIONSTART = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_ACTIONSTART >> 24)), INSTALLLOGMODE_ACTIONDATA = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_ACTIONDATA >> 24)), INSTALLLOGMODE_COMMONDATA = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_COMMONDATA >> 24)), INSTALLLOGMODE_PROPERTYDUMP = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_PROGRESS >> 24)), // log only INSTALLLOGMODE_VERBOSE = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_INITIALIZE >> 24)), // log only INSTALLLOGMODE_EXTRADEBUG = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_TERMINATE >> 24)), // log only INSTALLLOGMODE_LOGONLYONERROR = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_SHOWDIALOG >> 24)), // log only INSTALLLOGMODE_PROGRESS = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_PROGRESS >> 24)), // external handler only INSTALLLOGMODE_INITIALIZE = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_INITIALIZE >> 24)), // external handler only INSTALLLOGMODE_TERMINATE = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_TERMINATE >> 24)), // external handler only INSTALLLOGMODE_SHOWDIALOG = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_SHOWDIALOG >> 24)), // external handler only INSTALLLOGMODE_FILESINUSE = (1 << (INSTALLMESSAGE.INSTALLMESSAGE_FILESINUSE >> 24)), // external handler only } public enum INSTALLUILEVEL { INSTALLUILEVEL_NOCHANGE = 0, // UI level is unchanged INSTALLUILEVEL_DEFAULT = 1, // default UI is used INSTALLUILEVEL_NONE = 2, // completely silent installation INSTALLUILEVEL_BASIC = 3, // simple progress and error handling INSTALLUILEVEL_REDUCED = 4, // authored UI, wizard dialogs suppressed INSTALLUILEVEL_FULL = 5, // authored UI with wizards, progress, errors INSTALLUILEVEL_ENDDIALOG = 0x80, // display success/failure dialog at end of install INSTALLUILEVEL_PROGRESSONLY = 0x40, // display only progress dialog INSTALLUILEVEL_HIDECANCEL = 0x20, // do not display the cancel button in basic UI INSTALLUILEVEL_SOURCERESONLY = 0x100, // force display of source resolution even if quiet } [DllImport("msi.dll", CharSet = CharSet.Unicode, SetLastError = true)] static extern uint MsiInstallProduct(string packagePath, string commandLine); [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)] public delegate int InstallUIHandler( IntPtr pvContext, uint iMessageType, [MarshalAs(UnmanagedType.LPWStr)] string szMessage); [DllImport("msi.dll", CharSet = CharSet.Unicode, SetLastError = true)] static extern uint MsiSetExternalUI(InstallUIHandler puiHandler, INSTALLLOGMODE dwMessageFilter, IntPtr pvContext); [DllImport("msi.dll", SetLastError = true)] static extern int MsiSetInternalUI(INSTALLUILEVEL dwUILevel, ref IntPtr phWnd); } }
Пример вывода:
Action 10:11:18: INSTALL. Action 10:11:18: ValidateProductID. Action 10:11:18: CostInitialize. Computing space requirements Action 10:11:18: FileCost. Computing space requirements Action 10:11:18: CostFinalize. Computing space requirements Action 10:11:18: InstallValidate. Validating install Action 10:11:18: InstallInitialize. Action 10:11:18: GenerateScript. Generating script operations for action: Action 10:11:18: ProcessComponents. Updating component registration Action 10:11:18: MsiUnpublishAssemblies. Unpublishing assembly information Action 10:11:18: UnpublishFeatures. Unpublishing Product Features Action 10:11:18: RemoveRegistryValues. Removing system registry values Action 10:11:19: RemoveFiles. Removing files Action 10:11:19: InstallFiles. Copying new files Action 10:11:19: WriteRegistryValues. Writing system registry values Action 10:11:19: RegisterUser. Registering user Action 10:11:19: RegisterProduct. Registering product Action 10:11:19: MsiPublishAssemblies. Publishing assembly information Action 10:11:19: PublishFeatures. Publishing Product Features Action 10:11:19: PublishProduct. Publishing product information Action 10:11:19: InstallFinalize. Action 10:11:19: InstallInitialize. Action 10:11:19: ProcessComponents. Updating component registration Action 10:11:19: MsiUnpublishAssemblies. Unpublishing assembly information Action 10:11:19: UnpublishFeatures. Unpublishing Product Features Action 10:11:19: RemoveRegistryValues. Removing system registry values Action 10:11:19: RemoveFiles. Removing files Action 10:11:19: PublishProduct. Publishing product information Action 10:11:20: RollbackCleanup. Removing backup files
Информацию о том, какие события Windows Installer можно обрабатывать, см. в документации по MsiSetExternalUI
Content is retrieved from StackExchange API.
Auto-generated by ruso-archive tools.