示例#1
0
        public static OrphanComponent IsOrphanComponent(RegistryKey componentKey)
        {
            string[] productCodes = componentKey.GetValueNames();

            string          compName = Path.GetFileName(componentKey.Name);
            OrphanComponent orphan   = new OrphanComponent(compName);

            foreach (string p in productCodes)
            {
                // Ignore default value
                if (string.IsNullOrWhiteSpace(p) || p.Equals("@") || p.Equals("00000000000000000000000000000000"))
                {
                    continue;
                }

                Guid productCode   = p.MsiObfuscate();
                Guid componentGuid = compName.MsiObfuscate();

                IEnumerable <ProductInstallation> pis = ProductInstallation.GetProducts(productCode.ToString("B"), null, UserContexts.All);
                if ((pis == null) || (pis.Count() <= 0))
                {
                    string keyPath = componentKey.GetValue(p) as string;
                    Console.WriteLine($"Component '{componentGuid.ToString("B")}' in registry key '{componentKey.Name}' is registered to product '{productCode.ToString("B")} which is not installed with KeyPath='{keyPath}'");
                    orphan.NonExistingClients.Add(productCode.ToString("B"));
                }
            }

            return(orphan.NonExistingClients.Count > 0 ? orphan : null);
        }
示例#2
0
        public static List <OrphanComponent> DetectOrphanComponents()
        {
            List <OrphanComponent> orphans = new List <OrphanComponent>();

            using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
            {
                using (RegistryKey k = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components", false))
                {
                    if (k == null)
                    {
                        Console.WriteLine(@"Registry key doesn't exist: SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components");
                        return(null);
                    }

                    string[] componentCodes = k.GetSubKeyNames();
                    foreach (string c in componentCodes)
                    {
                        // Ignore default value
                        if (string.IsNullOrWhiteSpace(c) || c.Equals("@"))
                        {
                            continue;
                        }

                        using (RegistryKey k2 = k.OpenSubKey(c))
                        {
                            OrphanComponent orphan = OrphanComponent.IsOrphanComponent(k2);
                            if (orphan != null)
                            {
                                orphans.Add(orphan);
                            }
                        }
                    }
                }
            }

            return(orphans);
        }
示例#3
0
 static void Main(string[] args)
 {
     OrphanComponent.DetectOrphanComponents();
 }