简体   繁体   中英

How can I access the Recent Places list in Windows 7 using C#

Windows 7 introduced a Recent Places special folder in Explorer which shows you the recently accessed folders. This folder includes only folders, ie 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#?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM