コード例 #1
0
        public List <string> GetMatchingFiles()
        {
            var path = Path.Combine(
                Environment.GetEnvironmentVariable("AppData"),
                "Microsoft",
                "Windows",
                "Recent"
                );

            List <string> result = new List <string>();
            var           files  = Directory.EnumerateFiles(path, "*.lnk");

            foreach (var file in files)
            {
                // Typically happens with pendrives. Maybe we should treat this as a "Yes, please filter"?
                // Have a separate option to always delete no
                if (!File.Exists(file))
                {
                    Logger.Debug($"File {file} does not exist, skipping..");
                    continue;
                }

                string target;
                try {
                    target = LinkUtil.GetLnkTarget(_shell, file);
                }
                catch (COMException ex) {
                    if (LinkUtil.IsMissingDriveException(ex))
                    {
                        if (_filterMissingDrives)
                        {
                            result.Add(file);
                        }

                        continue;
                    }
                    else
                    {
                        throw;
                    }
                }

                foreach (var filter in _masks)
                {
                    var doFilter = target.StartsWith(filter, StringComparison.OrdinalIgnoreCase);
                    if (doFilter)
                    {
                        result.Add(file);
                        break;
                    }
                }
            }

            return(result);
        }
コード例 #2
0
        private string GetLnkTarget(byte[] linkBytes)
        {
            var tmp = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".lnk");

            try {
                try {
                    File.WriteAllBytes(tmp, linkBytes);
                    // TODO: Verify that we're dealing with a .lnk before writing this to disk etc?
                    return(LinkUtil.GetLnkTarget(_shell, tmp));
                }
                catch (IOException ex) {
                    Logger.Warn(ex);
                    return(string.Empty);
                }
            }
            finally {
                try {
                    File.Delete(tmp);
                }
                catch (Exception ex) {
                    Logger.Warn(ex);
                }
            }
        }