BlackWasp
Windows Forms Programming
.NET 1.1

Adding Files to My Recent Documents

If an application provides the facility to use files, Windows users expect to see recently loaded items in the My Recent Documents section of their Start menu. This functionality is missing from the .NET framework so the Windows API is used.

Referencing the API Function

The function that is required to provide the facility to add files to the My Recent Documents list, or to the Recent Items list in Microsoft Vista, is found in the Windows Application Programming Interface (API). There are several areas of API available to the .NET developer. The API required for this task is Shell32 and the name of the function is SHAddToRecentDocs.

NB: A technical definition of the SHAddToRecentDocs function can be on the Microsoft web site.

The first thing that we need to do is reference the InteropServices namespace. The InteropServices namespace provides the functionality required to reference the required Windows API. To add the references, add a new using statement at the beginning of the code in the same area as any existing using statements.

using System.Runtime.InteropServices;

With the link to the InteropServices defined, we can create a reference to the API function. This reference appears as a static function within a class.

[DllImport("Shell32.dll")]
static extern void SHAddToRecentDocs(int flags, string file);

As you can see, the SHAddToRecentDocs function accepts two parameters. The flags parameter describes to the API how the function is being used. The file parameter provides the detail of the file that is to be added to the list of recently opened documents. The function returns no value.

The flags parameter can accept one of two values. These are the named constants, SHARD_PATH and SHARD_PIDL. SHARD_PATH is used to indicate that the file parameter holds a simple string containing the file path and name. This is the variant of the function we will use. To make the code read more easily, let's define the constant, rather than using a magic number.

const int SHARD_PATH = 2;

Calling SHAddToRecentDocs

We have now created a reference to SHAddToRecentDocs that we can call from the class like any other function. The following code demonstrates this by adding the file "c:\test.txt" to the My Recent Documents list. To test this, create the file "test.txt" in the root of the C: drive. Ensure that this file is not listed in the recent documents list before executing the code. You can, of course, change the file name to the name of the file to add to the list.

SHAddToRecentDocs(SHARD_PATH, @"C:\test.txt");

Limitations

There are a couple of limitations to the use of the SHAddToRecentDocs API function. Firstly, Windows must recognise the file type extension of the file being listed. I explain how to register a file type in the article, Programmatically Checking and Setting File Types. The second limitation is that you cannot add executable files (.exe) to the recent documents list.

Link to this Page12 August 2006
RSS RSS Feed