Open Most Recent File

This is very simple, but I had never seen it anywhere else until I had to do it myself. It should save everyone some digging through the documentation.

To make your program re-open the most recently used file, simply add the following code to your App's InitInstance() between the calls to ParseCommandLine() and ProcessShellCommand().

// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);

// ================= Begin Inserted code =========================
// If a file is not specified on the command line, open the last file
if ( ! cmdInfo.m_strFileName.GetLength() )
{
   CString strFileName;
   if (m_pRecentFileList->GetDisplayName(strFileName, 0, "", 0, true))
   {
      cmdInfo.m_strFileName = strFileName;
      cmdInfo.m_nShellCommand =
      CCommandLineInfo::FileOpen;
   }
}
// ================== End Inserted code ==========================


// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;

Nico van Ravenstein adds:

There is a problem with opening the most recent file when the filepath is too long. The directory gets abbreviated by the function AbbreviateName like C:\some path\...\file.txt. Thats nice for a windows title or in the last used list but you can't open a file with it.

So a better way is this:

// Add this to your applications InitInstance function
//
// If a file is not specified on the command line, open the last file
if (!cmdInfo.m_strFileName.GetLength())
{
    if (m_pRecentFileList->m_nSize > 0 &&
    !m_pRecentFileList->m_arrNames[0].IsEmpty())
    {
        cmdInfo.m_strFileName   = m_pRecentFileList->m_arrNames[0];
        cmdInfo.m_nShellCommand = CCommandLineInfo::FileOpen; 
    }
}

Updated 4 April 1998



Comments

  • There's a much easier way to do this...

    Posted by Legacy on 07/28/2001 12:00am

    Originally posted by: Neal White III

    At the end of your CMainFrame::OnCreate() function, just call PostMessage(WM_COMMAND,ID_FILE_MRU_FILE1,0); no need to worry about exceptions either.

    Reply
  • How do I deal with the exception?

    Posted by Legacy on 12/02/1998 12:00am

    Originally posted by: abei

    This way is well fit for common case.But when the most recent file been
    removed,The application will abort abnormally. How I do?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds