예제 #1
0
        /// <summary>
        /// Uninstalls the MSI Product from this computer. Use the «Uninstall» method of the Win32_Product Wmi class.
        /// </summary>
        /// <param name="productCode">The MSI Product code of the MSI Product.</param>
        /// <returns>The MSI result code of the operation</returns>
        /// <exception cref="UnauthorizedAccessException">Thrown when credentials are not valid.</exception>
        /// <exception cref="Exception">Thrown in every other case.</exception>
        internal uint UninstallProduct(string productCode)
        {
            uint result = int.MaxValue;

            ManagementObjectCollection softwares = GetWmiQueryResult("Select * from Win32_Product where identifyingNumber like '{" + productCode + "}'");

            foreach (ManagementObject software in softwares)
            {
                result = (uint)software.InvokeMethod("Uninstall", null);
                if (MsiProduct.IsSuccess(result))
                {
                    RemoveProduct(productCode);
                }
            }

            return(result);
        }
예제 #2
0
 private void DisplayResult(uint resultCode)
 {
     if (MsiProduct.IsSuccess(resultCode))
     {
         if (MsiProduct.IsRebootNeeded(resultCode))
         {
             txtBxResult.Text = _localization.GetLocalizedString("SuccessPendingReboot");
         }
         else
         {
             txtBxResult.Text = _localization.GetLocalizedString("Success");
         }
         txtBxResult.BackColor = Color.LightGreen;
     }
     else
     {
         txtBxResult.Text      = _localization.GetLocalizedString("Failed") + "(" + resultCode.ToString() + ") : " + MsiProduct.GetErrorMessage(resultCode);
         txtBxResult.BackColor = Color.Orange;
     }
     txtBxResult.Refresh();
 }
        private void ReportUninstallResult(uint resultCode, DataGridViewRow row, MsiProduct uninstalledProduct)
        {
            row.Cells["ResultCode"].Value = resultCode;

            if (MsiProduct.IsSuccess(resultCode))
            {
                txtBxMessage.BackColor = Color.LightGreen;
                if (MsiProduct.IsRebootNeeded(resultCode))
                {
                    row.Cells["Result"].Value = _localization.GetLocalizedString("SuccessPendingReboot");
                    row.Cells["Result"].Style = _successRebootStyle;
                    txtBxMessage.Text         = _localization.GetLocalizedString("SuccessPendingReboot");
                    if (!UninstalledProducts.Contains(uninstalledProduct))
                    {
                        UninstalledProducts.Add(uninstalledProduct);
                    }
                }
                else
                {
                    row.Cells["Result"].Value = _localization.GetLocalizedString("Success");
                    row.Cells["Result"].Style = _successStyle;
                    txtBxMessage.Text         = _localization.GetLocalizedString("Success");
                    if (!UninstalledProducts.Contains(uninstalledProduct))
                    {
                        UninstalledProducts.Add(uninstalledProduct);
                    }
                }
            }
            else
            {
                txtBxMessage.BackColor    = Color.Orange;
                row.Cells["Result"].Value = _localization.GetLocalizedString("Failed") + "(" + resultCode + ")";
                row.Cells["Result"].Style = _failedStyle;
                txtBxMessage.Text         = MsiProduct.GetErrorMessage(resultCode);
            }
        }
        private void UninstallProducts()
        {
            foreach (DataGridViewRow row in dgvProducts.Rows)
            {
                try
                {
                    uint resultCode = 0;
                    if (!uint.TryParse(row.Cells["ResultCode"].Value.ToString(), out resultCode) || !MsiProduct.IsSuccess(resultCode))
                    {
                        MsiProduct currentProduct = (MsiProduct)row.Cells["Product"].Value;
                        row.Cells["Result"].Value = _localization.GetLocalizedString("InProgress");
                        uint result = TargetComputer.UninstallProduct(currentProduct.IdentifyingNumber);

                        ReportUninstallResult(result, row, currentProduct);
                    }
                }
                catch (Exception ex)
                {
                    txtBxMessage.Text      = _localization.GetLocalizedString("Failed") + " : " + ex.Message;
                    txtBxMessage.BackColor = Color.Orange;
                }
            }
        }