13

I am making a WPF Application in C# where I need to show the recent documents history (just like it happens in word, excel and even visual studio), showing the list the last 5 or 10 documents opened. I have absolutely no idea as to how I should go about it. Please help. And please be kind and gentle...I am an amatuer coder, and it is tough to digest high-tech talks as of now! :)

Gagan
  • 337
  • 3
  • 7
  • 18

6 Answers6

10

JumpList in WPF4 is awesome. This was all I needed to do:

<Application 
    x:Class="MyApp"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    <Application.Resources>
    </Application.Resources>
    <JumpList.JumpList>
        <JumpList ShowRecentCategory="True"/>
    </JumpList.JumpList>
</Application>
Sergey Aldoukhov
  • 22,316
  • 18
  • 72
  • 99
  • What are the JumpList for WinForm? – CallMeLaNN Feb 09 '11 at 10:08
  • @kmc Because JumpList (the code shown) displays a list of Most Recent Used documents, you webby ;) – Sergey Aldoukhov Aug 03 '11 at 02:37
  • 3
    Maybe i'm missing something, but this just creates a jump list and says to include the Recent category (Which is the default JumpList you get for free anyway). This does nothing for showing the Recent items IN the application? – Nick Oct 23 '15 at 14:25
  • 1
    This initializes the jump list but doesn't do anything with it. At the very least one must also invoke e.g. `AddToRecentCategory()` and `Apply()` whenever a file should be recorded as recent. Additionally, this information is only available in the Windows taskbar, not within the application itself. – mkjeldsen Apr 01 '21 at 17:24
  • Here is a more complete jump list answer: https://stackoverflow.com/a/25542492/482758. – mkjeldsen Apr 01 '21 at 17:30
4

This has a pretty nice walk through and sample

http://www.codeproject.com/KB/WPF/RecentFileList.aspx

Its good that it has both an xml file and registry store.

djeeg
  • 6,685
  • 3
  • 25
  • 28
3

My idea of solving this problem (as a beginner) was to retain all the file paths into a Queue of given maximum capacity and adding them at run-time into a menuItem...

2

You could just keep a list of the documents that the user opens. Store the list when the program exits and load it when the program launches. You could probably store a list of things in the program settings, or you could write it to a file (plain text or xml would work ok).

You'd have to create the submenu for "recent documents" dynamically by keeping a reference to the "recent documents" MenuItem, then adding and removing MenuItems from its Items collection. There's a discussion about that here: Add new menuitem to menu at runtime.

The library that was linked above by Shoban looks like a set of classes that do this for you. But, it's for winforms. If you're using wpf, you might have to write your own (though there are probably pre-made ones out there somewhere), but the winforms one will give you a good starting place.

You can also then create jumplists in win7's taskbar using the Windows API Code Pack for .Net.

Community
  • 1
  • 1
Benny Jobigan
  • 5,078
  • 2
  • 31
  • 41
1

Gagan, i have recently made a recent file menu in WPF C# and here is what i did:

-> to enable the jumplist functionality and start menu recent file menu i used the windows API shell routine like this:

[DllImport("shell32.dll")] //shell routine to enable jumplist and recenfiles public static extern void SHAddToRecentDocs( UInt32 uFlags, [MarshalAs(UnmanagedType.LPWStr)] String pv);

and call it like this: SHAddToRecentDocs(0x00000003, mFilePath);

-> Then to display recent file menu i used an xml file, stored recent files in that and parsed and displayed recent file in the menu.

PUG
  • 4,301
  • 13
  • 73
  • 115
1

You might be interested in the Writer sample application of the WPF Application Framework (WAF). It shows how to use and implement a recent file list which is shown in the file menu and on the start page.

jbe
  • 6,976
  • 1
  • 43
  • 34