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! :)
How to create Recent Documents History in C# in WPF Application
18.2k views Asked by Gagan AtThere are 6 answers
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.
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.
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>
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.
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 removingMenuItem
s from itsItems
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.