SHAddToRecentDocs doesn't add my file to recent documents

941 Views Asked by At

Double clicking on a file in Explorer correctly adds the file to the recent list for my application and I can open it again from the popup menu on my application which I have pinned on the start menu.

I've got a special file manager in the application so I am using SHAddToRecentDocs to add the projects opened in the application to recent files. But it just doesn't happen and I can't find what the problem is.

Here's what I got in the registry:

HKEY_CLASSES_ROOT\.abc\Content Type = application/MyApp
HKEY_CLASSES_ROOT\.abc\(Standard) = MyAppProjectFile

HKEY_CLASSES_ROOT\MyAppProjectFile\shell\open\command\(Standard) = "C:\MyApp\MyApp.exe" %1

HKEY_CLASSES_ROOT\Applications\MyApp.exe\shell\open\command\(Standard) = "C:\MyApp\MyApp.exe" %1

There are no other keys under HKCR\Applications\MyApp.exe.

Like I said, I can open applications by double clicking on them in Explorer, they get added to the recent documents and everything looks fine. I can open them from the popup fine.

My SHAddToRecentDocs call, which gets a correct path, doesn't seem to be doing anything at all. No link appears in the recent documents folder.

Here's the C# code I use to run SHAddToRecentDocs:

[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
static extern void SHAddToRecentDocs(ShellAddToRecentDocsFlags flags, string file);

[Flags]
public enum ShellAddToRecentDocsFlags
{
   Pidl = 0x001,
   Path = 0x002,
}

/// <summary> 
///   Adds the file to recent files list in windows.
/// </summary>
/// <param name="fullPath"> Name of the file. </param>
public static void AddFileToRecentFilesList(string fullPath)
{
   SHAddToRecentDocs(ShellAddToRecentDocsFlags.Path, fullPath);
}
1

There are 1 best solutions below

0
On

If turned out that a fix to a FxCop code warning was the reason this didn't work.

The ShellAddToRecentDocsFlags API was defined as follows:

[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
static extern void SHAddToRecentDocs(ShellAddToRecentDocsFlags flags, string file);

Changing it to the following fixed the issue:

[DllImport("Shell32.dll", BestFitMapping = false, ThrowOnUnmappableChar = true)]
static extern void SHAddToRecentDocs(ShellAddToRecentDocsFlags flags, [MarshalAs(UnmanagedType.LPStr)]string file);