Open most recent file (3)

This code sniplet will cause your program to open the last saved file when it is reopened. If the last saved file still exists that is.

This code differs from the other open recent file code examples because it shows an alternate method of obtaining the last MRU. We also take into account if the most recently saved file still exists (after all it may not).

I have no way to test if this will work with UNICODE, however it SHOULD. (:
This was compiled with VC5 and MFC4.2. Tested on Win95 OSR2 (no IE4 integration). This code should compile cleanly under warning level 4.

I originally started out using m_pRecentFileList however in my SDI test app m_pRecentFileList wasn't valid. So, I put in what I've been using for ages in one of my other apps. (:

To make your program open the last saved file when it restarts, simply add the following code directly above the: if (!ProcessShellCommand(cmdInfo)). (assuming an appwizard generated source file.) line in your app's InitInstance() function. (in the <appname>.cpp file)

The Code...


//////////////////////////////////////////////////////////////
//  //  //  //  //  //  //  //  //  //  //  //  //  //  //  //
//////////////////////////////////////////////////////////////
// Put this block directly before ....
//    // Dispatch commands specified on the command line
//    if (!ProcessShellCommand(cmdInfo))
//        return FALSE;
// ie. after cmdInfo is defined and initialized and before it is used...
    // -------------------------------------------------------------------
    // Open the most recently saved file. (First on the MRU list.)
    CString strLastPath = GetProfileString(_T("Recent File List"),_T("File1"),_T("<<NONE>>") );  // get the last file from the registry...
    if( strLastPath != _T("<<NONE>>")       // If we were able to get the last saved file.
     && cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew
//     && bLoadLast                       // and we want to load the last!
      )
//we need not account for cmdInfo.m_bRunAutomated and cmdInfo.m_bRunEmbedded as they are processed before we get here...
    {
        CFile f;
        if(f.Open(strLastPath, CFile::modeRead) )
        {                               // file is there, set to open!
            cmdInfo.m_nShellCommand = CCommandLineInfo::FileOpen;
            cmdInfo.m_strFileName = strLastPath;
            TRACE1(_T("*OpenLastFile: Setting Vars to Load Last File (%s)\n"), cmdInfo.m_strFileName);
        }
        else
        {                               // File doesn't Exist, So, Create New.
            cmdInfo.m_nShellCommand = CCommandLineInfo::FileNew;  // should be redundant, but what the heck.
            TRACE0(_T("*OpenLastFile: Setting Vars to FileNew\n") );
        }
    }
//////////////////////////////////////////////////////////////
//  //  //  //  //  //  //  //  //  //  //  //  //  //  //  //
//////////////////////////////////////////////////////////////

Questions and comments are always welcome!

Happy Codeing! (btw, why is a program listing called source code now? :P )

Last updated: 6 July 1998



Comments

  • Slight rearrangement...

    Posted by Legacy on 01/14/1999 12:00am

    Originally posted by: LALeonard

    I rearranged the logic a little on this, since there's no need to read from the Registry if m_nShellCommand
    is FileNew (also simplified a little):
    
    

    // Open the most recently saved file. (First on the MRU list.) Get the last
    // file from the registry. We need not account for cmdInfo.m_bRunAutomated and
    // cmdInfo.m_bRunEmbedded as they are processed before we get here.
    if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew) {
    CString sLastPath(GetProfileString("Recent File List", "File1"));

    if (! sLastPath.IsEmpty()) {
    CFile f;

    // If file is there, set to open!
    if (f.Open(sLastPath, CFile::modeRead)) {
    cmdInfo.m_nShellCommand = CCommandLineInfo::FileOpen;
    cmdInfo.m_strFileName = sLastPath;
    }
    }
    }

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

Top White Papers and Webcasts

  • 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 …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds