CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2007
    Posts
    23

    Accessing the recent file list

    In a MFC application you can add the recent files opened easily to the File menu item list. I have found functions for inserting items in the recent file list but is there a way to obtain the file names (and paths) for the recent file list?

    They are stored in the register and you can open a register key to read their values but it seems to me that there should be some easier way to do this.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Accessing the recent file list

    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2007
    Posts
    23

    Re: Accessing the recent file list

    I don't see how that answers the question?

    I found that article to when I googled it but after reading it I realized that it was not quite the same as my question.
    Last edited by Ergodyne; June 3rd, 2009 at 03:50 AM.

  4. #4
    Join Date
    Apr 2007
    Posts
    23

    Re: Accessing the recent file list

    Ok, so I figured it out by my self and will post the answer just for reference.

    For reading the last opened file I use the following

    Code:
    CString file = AfxGetApp()->GetProfileStringA(_T("Recent File List"), _T("File1"), NULL);
    _tcscpy(m_szFile1, file.GetString());
    where m_szFile1 is a char variable of length _MAX_PATH.

    Rewriting recent files to the registry uses

    Code:
    CMyApp* app = (CMyApp*) AfxGetApp();
    
    app->AddToRecentFileList(m_szFile1);
    
    // This is defined in CMyApp to rewrite the recent files to the registry
    app->UpdateRecentFiles();
    Where CMyApp is the main App (inherited from CWinApp).

    In the CMyApp I have defined the following function
    Code:
    class CMyApp : public CWinApp
    {
    public:
    	CMyApp();
    // Overrides
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(CMyApp)
    	public:
    	virtual BOOL InitInstance();
    	//}}AFX_VIRTUAL
    
    // Implementation
    public:
    	void UpdateRecentFiles(){m_pRecentFileList->WriteList();}
    
    };

  5. #5
    Join Date
    Nov 2007
    Posts
    613

    Re: Accessing the recent file list

    It's an undocumented solution.


    1. Derive a class from CRecentFileList, let's name it CMyRecentFileList.


    2. In you ckass derived from CWinApp, write your own implementation of the LoadStdProfileSettings. To do that, just copy the CWinApp::LoadStdProfileSettings from the MFC sources. In that implementation modify the code to create the recent file list object form CMyRecentFileList instead of CRecentFileList.

    3. From the inside of that class you'll be able to access the theApp.m_pRecentFileList->m_arrNames[i]. That array holds the list of the recent files. Add a public function to that class to retrieve it.

    4. You need to add the following header files: afxadv.h, afximpl.h.

    Be aware that this technique makes your code dependent of the version of the Visual Studio you're currently using. Because Microsoft did not document the CRecentFile class and the m_arrNames variable , they're free to rename them or replace them with something different in the future.
    That dependence is uncertain but it may happen.
    Last edited by srelu; June 3rd, 2009 at 06:58 AM.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Accessing the recent file list

    Quote Originally Posted by srelu View Post
    [SIZE=2]Be aware that this technique makes your code dependent of the version of the Visual Studio you're currently using. Because Microsoft did not document the CRecentFile class and the m_arrNames variable , they're free to rename them or replace them with something different in the future.
    That dependence is uncertain but it may happen.
    If you meant CRecentFileList clas then it is pretty good documented in MSDN: CRecentFileList Members
    And, BTW, what is the reason not to use CRecentFileList class rather than implement your own copy of it?
    Victor Nijegorodov

  7. #7
    Join Date
    Nov 2007
    Posts
    613

    Re: Accessing the recent file list

    Quote Originally Posted by VictorN View Post
    If you meant CRecentFileList clas then it is pretty good documented in MSDN: CRecentFileList Members
    And, BTW, what is the reason not to use CRecentFileList class rather than implement your own copy of it?
    You're right. The variable is public and it can be accessed directly as theApp.m_pRecentFileList->m_arrNames[i].

    I was misguided by the fact that I'm just working with an application where I was forced to write my own implementation of the all function call chain starting from OnOpenDocumentFile including the handling of the recent file list. My document names are folder names and I needed next to the document name additional switches (like IncludeSubfolders).
    Not to mention that the document/view architecture was designed to handle files, not folders.

  8. #8
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Accessing the recent file list

    EDIT: Never mind
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured