コード例 #1
0
        // TODO overhaul
        private DriveJunkNode GetJunkNodeFromLocation(string directory)
        {
            try
            {
                var dirInfo = new DirectoryInfo(directory);

                if (dirInfo.FullName.Contains(FullWindowsDirectoryName) || !dirInfo.Exists || dirInfo.Parent == null)
                {
                    return(null);
                }

                var newNode = new DriveDirectoryJunkNode(Path.GetDirectoryName(directory),
                                                         Path.GetFileName(directory), Uninstaller.DisplayName);
                newNode.Confidence.Add(ConfidencePart.ExplicitConnection);

                if (CheckAgainstOtherInstallers(dirInfo))
                {
                    newNode.Confidence.Add(ConfidencePart.DirectoryStillUsed);
                }

                return(newNode);
            }
            catch
            {
                return(null);
            }
        }
コード例 #2
0
        private IEnumerable <DriveJunkNode> GetUninstallerJunk()
        {
            if (!File.Exists(Uninstaller.UninstallerFullFilename))
            {
                return(Enumerable.Empty <DriveJunkNode>());
            }

            DriveJunkNode result;

            switch (Uninstaller.UninstallerKind)
            {
            case UninstallerType.InstallShield:
                var target = Path.GetDirectoryName(Uninstaller.UninstallerFullFilename);
                result = new DriveDirectoryJunkNode(Path.GetDirectoryName(target),
                                                    Path.GetFileName(target), Uninstaller.DisplayName);
                break;

            case UninstallerType.InnoSetup:
            case UninstallerType.Msiexec:
            case UninstallerType.Nsis:
                result = new DriveFileJunkNode(Path.GetDirectoryName(Uninstaller.UninstallerFullFilename),
                                               Path.GetFileName(Uninstaller.UninstallerFullFilename), Uninstaller.DisplayName);
                break;

            default:
                return(Enumerable.Empty <DriveJunkNode>());
            }

            result.Confidence.Add(ConfidencePart.ExplicitConnection);

            return(new[] { result });
        }
コード例 #3
0
        private IEnumerable <DriveJunkNode> SearchWerReports()
        {
            var output = new List <DriveJunkNode>();

            if (!Directory.Exists(Uninstaller.InstallLocation))
            {
                return(output);
            }

            List <string> appExecutables;

            try
            {
                appExecutables = Directory.GetFiles(Uninstaller.InstallLocation, "*.exe", SearchOption.AllDirectories)
                                 .Select(Path.GetFileName).ToList();
            }
            catch (UnauthorizedAccessException ex)
            {
                Console.WriteLine(ex);
                return(Enumerable.Empty <DriveJunkNode>());
            }

            var archives = new[]
            {
                WindowsTools.GetEnvironmentPath(Klocman.Native.CSIDL.CSIDL_COMMON_APPDATA),
                WindowsTools.GetEnvironmentPath(Klocman.Native.CSIDL.CSIDL_LOCAL_APPDATA)
            }.Select(x => Path.Combine(x, @"Microsoft\Windows\WER\ReportArchive")).Where(Directory.Exists);

            const string crashLabel = "AppCrash_";
            var          candidates = archives.SelectMany(s =>
            {
                try
                {
                    return(Directory.GetDirectories(s));
                }
                catch (IOException e)
                {
                    Debug.WriteLine(e.Message);
                }
                catch (UnauthorizedAccessException e)
                {
                    Debug.WriteLine(e.Message);
                }
                return(Enumerable.Empty <string>());
            });

            foreach (var candidate in candidates)
            {
                var startIndex = candidate.IndexOf(crashLabel, StringComparison.InvariantCultureIgnoreCase);
                if (startIndex <= 0)
                {
                    continue;
                }
                startIndex = startIndex + crashLabel.Length;

                var count = candidate.IndexOf('_', startIndex) - startIndex;
                if (count <= 1)
                {
                    continue;
                }

                var filename = candidate.Substring(startIndex, count);

                if (appExecutables.Any(x => x.StartsWith(filename, StringComparison.InvariantCultureIgnoreCase)))
                {
                    var node = new DriveDirectoryJunkNode(Path.GetDirectoryName(candidate), Path.GetFileName(candidate),
                                                          Uninstaller.DisplayName);
                    node.Confidence.Add(ConfidencePart.ExplicitConnection);
                    output.Add(node);
                }
            }

            return(output);
        }
コード例 #4
0
        private IEnumerable <DriveJunkNode> FindJunkRecursively(DirectoryInfo directory, int level = 0)
        {
            var results = new List <DriveJunkNode>();

            try
            {
                var dirs = directory.GetDirectories();

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

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

                    DriveJunkNode newNode = null;
                    if (generatedConfidence.Any())
                    {
                        newNode = new DriveDirectoryJunkNode(directory.FullName, dir.Name, Uninstaller.DisplayName);
                        newNode.Confidence.AddRange(generatedConfidence);

                        if (CheckAgainstOtherInstallers(dir))
                        {
                            newNode.Confidence.Add(ConfidencePart.DirectoryStillUsed);
                        }

                        results.Add(newNode);
                    }

                    if (level >= 1)
                    {
                        continue;
                    }

                    var junkNodes = FindJunkRecursively(dir, level + 1).ToList();
                    results.AddRange(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.FullName))))
                            {
                                newNode.Confidence.Add(ConfidencePart.AllSubdirsMatched);
                            }
                        }
                    }
                }
            }
            catch
            {
                if (Debugger.IsAttached)
                {
                    throw;
                }
            }

            return(results);
        }