示例#1
0
        public IEnumerable <IJunkResult> FindJunk(ApplicationUninstallerEntry target)
        {
            var results = new List <RegistryKeyJunk>();

            using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(TracingKey))
            {
                if (key != null)
                {
                    foreach (var subKeyName in key.GetSubKeyNames())
                    {
                        var i = subKeyName.LastIndexOf('_');
                        if (i <= 0)
                        {
                            continue;
                        }

                        var str = subKeyName.Substring(0, i);

                        var conf = ConfidenceGenerators.GenerateConfidence(str, Path.Combine(FullTracingKey, subKeyName), 0, target).ToList();
                        if (conf.Any())
                        {
                            var node = new RegistryKeyJunk(Path.Combine(FullTracingKey, subKeyName), target, this);
                            node.Confidence.AddRange(conf);
                            results.Add(node);
                        }
                    }
                }
            }

            ConfidenceGenerators.TestForSimilarNames(target, _allEntries, results.Select(x => new KeyValuePair <JunkResultBase, string>(x, x.RegKeyName)).ToList());

            return(results.Cast <IJunkResult>());
        }
示例#2
0
        protected IEnumerable <ConfidenceRecord> GenerateConfidence(string itemName, string itemParentPath, int level)
        {
            var baseOutput = ConfidenceGenerators.GenerateConfidence(itemName, itemParentPath, level, _uninstaller).ToList();

            if (!baseOutput.Any(x => x.Change > 0))
            {
                return(Enumerable.Empty <ConfidenceRecord>());
            }

            if (UninstallToolsGlobalConfig.QuestionableDirectoryNames.Contains(itemName, StringComparison.OrdinalIgnoreCase))
            {
                baseOutput.Add(ConfidenceRecords.QuestionableDirectoryName);
            }

            return(baseOutput);
        }
示例#3
0
        public override IEnumerable <IJunkResult> FindJunk(ApplicationUninstallerEntry target)
        {
            if (string.IsNullOrEmpty(target.InstallLocation))
            {
                yield break;
            }

            var otherUninstallers = GetOtherUninstallers(target).ToList();

            using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
                       @"SYSTEM\CurrentControlSet\Services\EventLog\Application"))
            {
                if (key == null)
                {
                    yield break;
                }

                var query = from name in key.GetSubKeyNames()
                            let m = ConfidenceGenerators.MatchStringToProductName(target, name)
                                    where m >= 0 && m < 3
                                    //orderby m ascending
                                    select name;

                foreach (var result in query)
                {
                    using (var subkey = key.OpenSubKey(result))
                    {
                        var exePath = subkey?.GetStringSafe("EventMessageFile");
                        if (string.IsNullOrEmpty(exePath) || !PathTools.SubPathIsInsideBasePath(target.InstallLocation, Path.GetDirectoryName(exePath), true))
                        {
                            continue;
                        }

                        var node = new RegistryKeyJunk(subkey.Name, target, this);
                        // Already matched names above
                        node.Confidence.Add(ConfidenceRecords.ProductNamePerfectMatch);

                        if (otherUninstallers.Any(x => PathTools.SubPathIsInsideBasePath(x.InstallLocation, Path.GetDirectoryName(exePath), true)))
                        {
                            node.Confidence.Add(ConfidenceRecords.DirectoryStillUsed);
                        }

                        yield return(node);
                    }
                }
            }
        }
        public override IEnumerable <IJunkResult> FindJunk(ApplicationUninstallerEntry target)
        {
            var results = new List <FileSystemJunk>();

            if (!string.IsNullOrEmpty(target.InstallLocation))
            {
                results.AddRange(GetLinksPointingToLocation(entry => entry.InstallLocation, target)
                                 .DoForEach(x => x.Confidence.Add(ConfidenceRecords.ExplicitConnection)));
            }

            if (target.UninstallerKind == UninstallerType.Steam)
            {
                results.AddRange(GetLinksPointingToSteamApp(target));
            }
            else
            {
                if (!string.IsNullOrEmpty(target.UninstallerFullFilename))
                {
                    results.AddRange(GetLinksPointingToLocation(entry => entry.UninstallerFullFilename, target)
                                     .DoForEach(x => x.Confidence.Add(ConfidenceRecords.ExplicitConnection)));
                }

                if (!string.IsNullOrEmpty(target.UninstallerLocation))
                {
                    var exceptUninstallerShortcut = GetLinksPointingToLocation(entry => entry.UninstallerLocation, target)
                                                    .Where(possibleResult => results.All(result => !PathTools.PathsEqual(result.Path, possibleResult.Path)))
                                                    .ToList();

                    results.AddRange(exceptUninstallerShortcut);
                }
            }

            // Remove shortcuts that we aren't sure about
            foreach (var junkNode in results.ToList())
            {
                var name = Path.GetFileNameWithoutExtension(junkNode.Path.Name);
                junkNode.Confidence.AddRange(ConfidenceGenerators.GenerateConfidence(name, target));

                if (junkNode.Confidence.IsEmpty)
                {
                    results.Remove(junkNode);
                }
            }

            return(results.Cast <IJunkResult>());
        }
        private IEnumerable <RegistryKeyJunk> FindJunkRecursively(RegistryKey softwareKey, int level = -1)
        {
            var returnList = new List <RegistryKeyJunk>();

            try
            {
                // Don't try to scan root keys
                if (level > -1)
                {
                    var keyName    = Path.GetFileName(softwareKey.Name);
                    var keyDir     = Path.GetDirectoryName(softwareKey.Name);
                    var confidence =
                        ConfidenceGenerators.GenerateConfidence(keyName, keyDir, level, _uninstaller).ToList();

                    // Check if application's location is explicitly mentioned in any of the values
                    if (softwareKey.TryGetValueNames().Any(valueName => TestValueForMatches(softwareKey, valueName)))
                    {
                        confidence.Add(ConfidenceRecord.ExplicitConnection);
                    }

                    if (confidence.Any())
                    {
                        // TODO Add extra confidence if the key is, or will be empty after junk removal
                        var newNode = new RegistryKeyJunk(softwareKey.Name, _uninstaller, this);
                        newNode.Confidence.AddRange(confidence);
                        returnList.Add(newNode);
                    }
                }

                // Limit recursion depth
                if (level <= 1)
                {
                    foreach (var subKeyName in softwareKey.GetSubKeyNames())
                    {
                        if (KeyBlacklist.Contains(subKeyName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            continue;
                        }

                        using (var subKey = softwareKey.OpenSubKey(subKeyName, false))
                        {
                            if (subKey != null)
                            {
                                returnList.AddRange(FindJunkRecursively(subKey, level + 1));
                            }
                        }
                    }
                }
            }
            // Reg key invalid
            catch (ArgumentException)
            {
            }
            catch (SecurityException)
            {
            }
            catch (ObjectDisposedException)
            {
            }

            return(returnList);
        }
        public override IEnumerable <IJunkResult> FindJunk(ApplicationUninstallerEntry target)
        {
            var isStoreApp = target.UninstallerKind == UninstallerType.StoreApp;

            if (isStoreApp && string.IsNullOrEmpty(target.RatingId))
            {
                throw new ArgumentException("StoreApp entry has no ID");
            }

            if (isStoreApp)
            {
                foreach (var regAppEntry in _regAppsValueCache)
                {
                    if (regAppEntry.AppName == null)
                    {
                        continue;
                    }

                    if (string.Equals(regAppEntry.AppName, target.RatingId, StringComparison.OrdinalIgnoreCase))
                    {
                        // Handle the value under RegisteredApps itself
                        var regAppResult = new RegistryValueJunk(regAppEntry.RegAppFullPath, regAppEntry.ValueName, target, this);
                        regAppResult.Confidence.Add(ConfidenceRecords.ExplicitConnection);
                        yield return(regAppResult);

                        // Handle the key pointed at by the value
                        var appEntryKey = new RegistryKeyJunk(regAppEntry.AppKey, target, this);
                        appEntryKey.Confidence.Add(ConfidenceRecords.ExplicitConnection);
                        appEntryKey.Confidence.Add(ConfidenceRecords.IsStoreApp);
                        yield return(appEntryKey);
                    }
                }
            }
            else
            {
                foreach (var regAppEntry in _regAppsValueCache)
                {
                    if (regAppEntry.AppName != null)
                    {
                        continue;
                    }

                    var generatedConfidence = ConfidenceGenerators.GenerateConfidence(regAppEntry.ValueName, target).ToList();

                    if (generatedConfidence.Count > 0)
                    {
                        // Handle the value under RegisteredApps itself
                        var regAppResult = new RegistryValueJunk(regAppEntry.RegAppFullPath, regAppEntry.ValueName, target, this);
                        regAppResult.Confidence.AddRange(generatedConfidence);
                        yield return(regAppResult);

                        // Handle the key pointed at by the value
                        const string capabilitiesSubkeyName = "\\Capabilities";
                        if (regAppEntry.TargetSubKeyPath.EndsWith(capabilitiesSubkeyName, StringComparison.Ordinal))
                        {
                            var capabilitiesKeyResult = new RegistryKeyJunk(regAppEntry.TargetFullPath, target, this);
                            capabilitiesKeyResult.Confidence.AddRange(generatedConfidence);
                            yield return(capabilitiesKeyResult);

                            var ownerKey = regAppEntry.TargetFullPath.Substring(0,
                                                                                regAppEntry.TargetFullPath.Length - capabilitiesSubkeyName.Length);

                            var subConfidence = ConfidenceGenerators.GenerateConfidence(Path.GetFileName(ownerKey),
                                                                                        target).ToList();
                            if (subConfidence.Count > 0)
                            {
                                var subResult = new RegistryKeyJunk(ownerKey, target, this);
                                subResult.Confidence.AddRange(subConfidence);
                                yield return(subResult);
                            }
                        }
                    }
                }
            }
        }
        private IEnumerable <FileSystemJunk> FindJunkRecursively(DirectoryInfo directory, ApplicationUninstallerEntry uninstaller, int level = 0)
        {
            var added = new List <FileSystemJunk>();
            IEnumerable <FileSystemJunk> results = added;

            try
            {
                var dirs = directory.GetDirectories();

                foreach (var dir in dirs)
                {
                    if (UninstallToolsGlobalConfig.IsSystemDirectory(dir))
                    {
                        continue;
                    }

                    var generatedConfidence = GenerateConfidence(dir.GetNameWithoutExtension(), directory.FullName, uninstaller, level).ToList();

                    FileSystemJunk newNode = null;
                    if (generatedConfidence.Any())
                    {
                        newNode = new FileSystemJunk(dir, uninstaller, this);
                        newNode.Confidence.AddRange(generatedConfidence);

                        if (CheckIfDirIsStillUsed(dir.FullName, GetOtherInstallLocations(uninstaller)))
                        {
                            newNode.Confidence.Add(ConfidenceRecords.DirectoryStillUsed);
                        }

                        added.Add(newNode);
                    }

                    if (level > 1)
                    {
                        continue;
                    }

                    var junkNodes = FindJunkRecursively(dir, uninstaller, level + 1).ToList();
                    results = results.Concat(junkNodes);

                    if (newNode != null)
                    {
                        // Check if the directory will have nothing left after junk removal.
                        if (!dir.GetFiles().Any())
                        {
                            var subDirs = dir.GetDirectories();
                            if (!subDirs.Any() || subDirs.All(d => junkNodes.Any(y => PathTools.PathsEqual(d.FullName, y.Path.FullName))))
                            {
                                newNode.Confidence.Add(ConfidenceRecords.AllSubdirsMatched);
                            }
                        }
                    }
                }

                ConfidenceGenerators.TestForSimilarNames(uninstaller, AllUninstallers, added.Select(x => new KeyValuePair <JunkResultBase, string>(x, x.Path.GetNameWithoutExtension())).ToList());
            }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    throw;
                }
                Console.WriteLine(ex);
            }

            return(results);
        }