1

Windows 7 introduced a Recent Places special folder in Explorer which shows you the recently accessed folders. This folder includes only folders, i.e. it excludes files.

This is different from the Environment.SpecialFolder.Recent folder and as far as I can see both CSIDL and KNOWNFOLDER don't list a Recent Places folder.

How can I get the contents of the Recent Places special folder using C#?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Patrick Klug
  • 14,056
  • 13
  • 71
  • 118
  • Are you refering to `C:\Users\\AppData\Roaming\Microsoft\Windows\Recent`? – Oscar Mederos May 03 '11 at 02:21
  • no, I mean the Recent Places feature in Explorer (normally just below the Desktop button in the top left, under Favorites) – Patrick Klug May 03 '11 at 02:23
  • For some reason I can't see it ;(. Anyway, did you take a look at the path I gave you to see if both places contain the same shortcuts? – Oscar Mederos May 03 '11 at 02:25
  • @OscarMederos: maybe. The Recent list contains both files and folders while the Recent Places has only directories but I guess I could manually filter and only get the links to directories. Will try. just thought that there might be a new dedicated special folder. – Patrick Klug May 03 '11 at 02:35
  • 1
    It surely is a virtual folder. Not well supported by .NET – Hans Passant May 03 '11 at 03:04

1 Answers1

2

As there doesn't seem to be any direct access to this 'virtual folder' I used a workaround.

string[] GetRecentPlaces()
{
    var places = new List<string>();
    foreach (var lnk in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Recent), "*.lnk"))
    {
        var path = LinkHelper.ResolveShortcut(lnk);
        if (Directory.Exists(path))
        {
            places.Add(path);
        }
    }
    return places.ToArray();
}

where LinkHelper is taken from this answer: How to resolve a .lnk in C#

EDIT: changed from using file attributes to Directory.Exists as FileAttribute.Directory flag doesn't seem to be always set correctly.

Community
  • 1
  • 1
Patrick Klug
  • 14,056
  • 13
  • 71
  • 118