Example #1
0
        private void CheckInstalled()
        {
            //1. Get upgrade code
            string upgradeCode = string.Empty;

            using (MsiConnection conn = new MsiConnection(msiFile)) {
                conn.Open();

                using (MsiCommand cmd = new MsiCommand("SELECT Property, Value FROM Property WHERE Property='UpgradeCode'", conn)) {
                    using (MsiDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            upgradeCode = reader.GetString(1);
                        }
                    }
                }
            }
            //2. Check any software is already installed
            productIds = EnumRelatedProducts(upgradeCode);
        }
Example #2
0
        private void GetInstalledFeatures()
        {
            //1. Get product code
            string productCode = string.Empty;

            using (MsiConnection conn = new MsiConnection(msiFile)) {
                conn.Open();

                using (MsiCommand cmd = new MsiCommand("SELECT Property, Value FROM Property WHERE Property='ProductCode'", conn)) {
                    using (MsiDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            productCode = reader.GetString(1);
                        }
                    }
                }
            }
            //2. get all features
            EnumRelatedFeatures(productCode);
        }
Example #3
0
        public PackageWriter(IPackage package, string filename)
        {
            _package = package;

            _connection = new MsiConnection(filename);
        }
Example #4
0
        private void LoadFeatures()
        {
            using (MsiConnection conn = new MsiConnection(msiFile)) {
                conn.Open();

                // GET ALL FEATURES
                features.Clear();
                Dictionary <string, MsiFeature> dicFeatures = new Dictionary <string, MsiFeature>();
                using (MsiCommand cmd = new MsiCommand("SELECT Feature, Feature_Parent, Title, Description, Display, Directory_ FROM Feature", conn)) {
                    using (MsiDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            MsiFeature feature = new MsiFeature();
                            feature.SetId(reader.GetString(0));
                            feature.ParentId = reader.GetString(1);
                            feature.SetTitle(reader.GetString(2));
                            feature.SetDescription(reader.GetString(3));
                            feature.SetDisplay(reader.GetInteger(4));
                            feature.SetDirectory(reader.GetString(5));

                            dicFeatures.Add(feature.Id, feature);
                            features.Add(feature);
                        }
                    }
                }

                // CREATE FEATURE TREE
                // 1. Set all feature parent and child nodes
                foreach (MsiFeature feature in features)
                {
                    if (String.IsNullOrEmpty(feature.ParentId) == false)
                    {
                        feature.SetParent(dicFeatures[feature.ParentId]);
                        if (feature.ParentFeature.ChildFeatures == null)
                        {
                            feature.ParentFeature.SetChildFeature(new List <MsiFeature>());
                        }

                        feature.ParentFeature.ChildFeatures.Add(feature);
                    }
                }
                // 2. Get only root nodes
                featureTree.Clear();
                foreach (MsiFeature feature in features)
                {
                    if (feature.ParentFeature == null)
                    {
                        featureTree.Add(feature);
                    }
                }

                // SET INSTALLDIR
                using (MsiCommand cmd = new MsiCommand("SELECT Value FROM Property WHERE Property='INSTALLDIR'", conn)) {
                    using (MsiDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            installDir = reader.GetString(0);
                        }
                    }
                }

                // CALCULATE FEATURE COST
                // 1. Get all components
                Dictionary <string, MsiComponent> files = new Dictionary <string, MsiComponent>();
                using (MsiCommand cmd = new MsiCommand("SELECT Component_, FileSize FROM File", conn)) {
                    using (MsiDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            MsiComponent component = new MsiComponent();
                            component.Id       = reader.GetString(0);
                            component.FileSize = reader.GetInteger(1);

                            files.Add(component.Id, component);
                        }
                    }
                }

                // 2. Get feature component mapping
                Dictionary <string, List <string> > featureComponents = new Dictionary <string, List <string> >();
                using (MsiCommand cmd = new MsiCommand("SELECT Feature_, Component_ FROM FeatureComponents", conn)) {
                    using (MsiDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            string feature = reader.GetString(0);
                            if (featureComponents.ContainsKey(feature) == false)
                            {
                                featureComponents.Add(feature, new List <string>());
                            }
                            featureComponents[feature].Add(reader.GetString(1));
                        }
                    }
                }

                // 3. calculate feature cost
                foreach (string feature in featureComponents.Keys)
                {
                    int cost = 0;
                    foreach (string component in featureComponents[feature])
                    {
                        if (files.ContainsKey(component))
                        {
                            cost += files[component].FileSize;
                        }
                    }

                    dicFeatures[feature].SetFeatureCost(cost);
                }

                // 4. Get total feature cost
                foreach (MsiFeature feature in featureTree)
                {
                    CalculateTotalFeatureCost(feature);
                }
            }
        }