Open Most Recent File


How to Help Your Business Become an AI Early Adopter

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;

// ================= 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 =
// ================== 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 &&
        cmdInfo.m_strFileName   = m_pRecentFileList->m_arrNames[0];
        cmdInfo.m_nShellCommand = CCommandLineInfo::FileOpen; 

Updated 4 April 1998


  • There are no comments yet. Be the first to comment!

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date