private void dgvProducts_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
        {
            if (e.Column == dgvProducts.Columns["InstallDate"])
            {
                string date1 = e.CellValue1.ToString();
                string date2 = e.CellValue2.ToString();
                date1        = date1.Substring(6) + date1.Substring(3, 2) + date1.Substring(0, 2);
                date2        = date2.Substring(6) + date2.Substring(3, 2) + date2.Substring(0, 2);
                e.SortResult = String.Compare(date1, date2);
                e.Handled    = true;
            }
            else if (e.Column == dgvProducts.Columns["Version"])
            {
                string version1 = e.CellValue1.ToString();
                string version2 = e.CellValue2.ToString();

                version1     = MsiProduct.GetConcatenatedVersion(version1);
                version2     = MsiProduct.GetConcatenatedVersion(version2);
                e.SortResult = String.Compare(version1, version2);
                e.Handled    = true;
            }
            else
            {
                e.Handled = false;
            }
        }
        private List <MsiProduct> FilterInstalledProducts(List <MsiProduct> allInstalledProducts, string pattern, string exception)
        {
            List <MsiProduct> productsToDisplay = new List <MsiProduct>();
            List <string>     productToFind     = MsiProduct.SplitMsiProductCodes(pattern);
            List <string>     exceptions        = MsiProduct.SplitMsiProductCodes(exception);

            foreach (MsiProduct installedProduct in allInstalledProducts)
            {
                foreach (string product in productToFind)
                {
                    if (MsiProduct.PatternMatchMsiCode(installedProduct.IdentifyingNumber, product))
                    {
                        bool displayIt = true;
                        foreach (string currentException in exceptions)
                        {
                            if (MsiProduct.PatternMatchMsiCode(installedProduct.IdentifyingNumber, currentException))
                            {
                                displayIt = false;
                                break;
                            }
                        }
                        if (displayIt)
                        {
                            productsToDisplay.Add(installedProduct);
                            break;
                        }
                    }
                }
            }

            return(productsToDisplay);
        }
        private void txtBxExceptions_TextChanged(object sender, EventArgs e)
        {
            int carretPosition = txtBxExceptions.SelectionStart;
            int textLength     = txtBxExceptions.TextLength;

            txtBxExceptions.Text           = MsiProduct.RemoveUnvantedCharacters(txtBxExceptions.Text);
            carretPosition                -= textLength - txtBxExceptions.TextLength;
            txtBxExceptions.SelectionStart = System.Math.Max(0, System.Math.Min(carretPosition, txtBxExceptions.TextLength));
        }
 private void ShowProductDetails(int rowIndex)
 {
     try
     {
         MsiProduct           selectedProduct = (MsiProduct)dgvProducts.Rows[rowIndex].Cells["Product"].Value;
         FrmProductProperties properties      = new FrmProductProperties(selectedProduct);
         properties.ShowDialog();
     }
     catch (Exception) {  }
 }
 private void dgvProducts_SelectionChanged(object sender, EventArgs e)
 {
     txtBxMessage.Text = String.Empty;
     if (dgvProducts.SelectedRows != null && dgvProducts.SelectedRows.Count == 1)
     {
         uint errorCode;
         if (uint.TryParse(dgvProducts.SelectedRows[0].Cells["ResultCode"].Value.ToString(), out errorCode))
         {
             txtBxMessage.Text = MsiProduct.GetErrorMessage(errorCode);
         }
     }
 }
 private void dgvProducts_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
 {
     try
     {
         if (e.RowIndex >= 0)
         {
             MsiProduct           selectedProduct = (MsiProduct)dgvProducts.Rows[e.RowIndex].Cells["Product"].Value;
             FrmProductProperties properties      = new FrmProductProperties(selectedProduct);
             properties.ShowDialog();
         }
     }
     catch (Exception) {  }
 }
예제 #7
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);
        }
        private void RemoveProduct(MsiProduct productToRemove)
        {
            DataGridViewRow rowToRemove = null;

            foreach (DataGridViewRow row in dgvProducts.Rows)
            {
                if ((row.Cells["Product"].Value as MsiProduct) == productToRemove)
                {
                    rowToRemove = row;
                    break;
                }
            }
            if (rowToRemove != null)
            {
                dgvProducts.Rows.Remove(rowToRemove);
            }
        }
        internal FrmUninstallProduct(Computer targetComputer, List <MsiProduct> targetProducts)
        {
            InitializeComponent();

            _successStyle.BackColor       = Color.LawnGreen;
            _successRebootStyle.BackColor = Color.Orange;
            _failedStyle.BackColor        = Color.Red;

            TargetComputer     = targetComputer;
            txtBxComputer.Text = targetComputer.ComputerName;
            foreach (MsiProduct msiProduct in targetProducts)
            {
                int index = dgvProducts.Rows.Add();
                dgvProducts.Rows[index].Cells["Product"].Value     = msiProduct;
                dgvProducts.Rows[index].Cells["ProductName"].Value = msiProduct.Name;
                dgvProducts.Rows[index].Cells["Version"].Value     = msiProduct.Version;
                dgvProducts.Rows[index].Cells["InstallDate"].Value = MsiProduct.GetFormattedInstallDate(msiProduct.InstallDate);
                dgvProducts.Rows[index].Cells["Result"].Value      = String.Empty;
                dgvProducts.Rows[index].Cells["ResultCode"].Value  = String.Empty;
            }
        }
예제 #10
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 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;
                }
            }
        }
        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);
            }
        }
예제 #13
0
        /// <summary>
        /// Retrieves MSI Installed Products by querying the Win32_Product WMI class. Fill the <see cref="_products"/> field.
        /// </summary>
        /// <exception cref="UnauthorizedAccessException">Thrown when credentials are not valid.</exception>
        /// <exception cref="Exception">Thrown in every other case.</exception>
        private void GetInstalledProducts()
        {
            if (!Destroying)
            {
                ProductsRetrievalInProgress = true;
                _products.Clear();

                try
                {
                    LastErrorThrown = null;
                    ManagementObjectCollection results = GetWmiQueryResult("Select * from Win32_Product");

                    foreach (ManagementObject result in results)
                    {
                        if (Destroying)
                        {
                            break;
                        }
                        try
                        {
                            MsiProduct product = new MsiProduct(GetFormattedIdentifyingNumber(result["identifyingNumber"].ToString()), result["Name"].ToString(), result["Version"].ToString());

                            _products.Add(product);

                            product.Caption         = GetProperty("Caption", result);
                            product.Description     = GetProperty("Description", result);
                            product.HelpLink        = GetProperty("HelpLink", result);
                            product.InstallLocation = GetProperty("InstallLocation", result);
                            product.InstallSource   = GetProperty("InstallSource", result);
                            product.Language        = GetProperty("Language", result);
                            product.LocalPackage    = GetProperty("LocalPackage", result);
                            product.PackageCache    = GetProperty("PackageCache", result);
                            product.PackageCode     = GetProperty("PackageCode", result);
                            product.PackageName     = GetProperty("PackageName", result);
                            product.ProductID       = GetProperty("ProductID", result);
                            product.RegOwner        = GetProperty("RegOwner", result);
                            product.Transform       = GetProperty("Transform", result);
                            product.UrlInfoAbout    = GetProperty("UrlInfoAbout", result);
                            product.UrlUpdateInfo   = GetProperty("UrlUpdateInfo", result);
                            product.Vendor          = GetProperty("Vendor", result);
                            product.InstallDate     = GetProperty("InstallDate", result);

                            string assignmentType = GetProperty("AssignmentType", result);
                            switch (assignmentType)
                            {
                            case "0":
                                product.AssignmentType = MsiProduct.Assignment_Type.User;
                                break;

                            case "1":
                                product.AssignmentType = MsiProduct.Assignment_Type.Computer;
                                break;

                            default:
                                product.AssignmentType = MsiProduct.Assignment_Type.Unknown;
                                break;
                            }
                            string installState = GetProperty("InstallState", result);
                            switch (installState)
                            {
                            case "-6":
                                product.InstallState = MsiProduct.Install_State.Bad_Configuration;
                                break;

                            case "-2":
                                product.InstallState = MsiProduct.Install_State.Invalid_Argument;
                                break;

                            case "-1":
                                product.InstallState = MsiProduct.Install_State.Unknown_Package;
                                break;

                            case "1":
                                product.InstallState = MsiProduct.Install_State.Advertised;
                                break;

                            case "2":
                                product.InstallState = MsiProduct.Install_State.Absent;
                                break;

                            case "5":
                                product.InstallState = MsiProduct.Install_State.Installed;
                                break;

                            default:
                                product.InstallState = MsiProduct.Install_State.Unknown;
                                break;
                            }
                        }
                        catch (Exception) { }
                    }
                }
                catch (Exception ex)
                {
                    LastErrorThrown = ex;
                }
            }

            ProductsRetrievalInProgress = false;
            if (ProductsRetrieved != null && !Destroying)
            {
                ProductsRetrieved(this);
            }
        }
예제 #14
0
 internal FrmProductProperties(MsiProduct product)
 {
     InitializeComponent();
     propertyGrid1.SelectedObject = product;
 }
        private void DisplayProductForComputer(Computer computer)
        {
            Action displayProductsAction = () =>
            {
                try
                {
                    dgvProducts.Rows.Clear();
                    if (!computer.ProductsRetrievalInProgress)
                    {
                        List <DataGridViewRow> rows = new List <DataGridViewRow>();

                        if (String.IsNullOrEmpty(txtBxPattern.Text))
                        {
                            txtBxPattern.Text = "%";
                        }

                        List <MsiProduct> displayedProducts = FilterInstalledProducts(computer.Products, txtBxPattern.Text, txtBxExceptions.Text);
                        foreach (MsiProduct product in displayedProducts)
                        {
                            DataGridViewRow row = new DataGridViewRow();
                            row.CreateCells(dgvProducts, new Object[] { product, product.IdentifyingNumber, product.Name, product.Version, MsiProduct.GetFormattedInstallDate(product.InstallDate) });
                            rows.Add(row);
                        }
                        dgvProducts.Rows.AddRange(rows.ToArray());
                        dgvProducts.ClearSelection();
                        UpdateProductCount(computer, displayedProducts.Count);
                        if (dgvProducts.SortedColumn != null)
                        {
                            dgvProducts.Sort(dgvProducts.SortedColumn, dgvProducts.SortOrder == SortOrder.Ascending ? ListSortDirection.Ascending : ListSortDirection.Descending);
                        }
                        else
                        {
                            dgvProducts.Sort(dgvProducts.Columns["ProductName"], ListSortDirection.Ascending);
                        }
                    }
                }
                catch (Exception) {  }
            };

            Invoke(displayProductsAction);
        }