Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / MFC
Article

CFileWatch

Rate me:
Please Sign up or sign in to vote.
4.68/5 (9 votes)
1 Apr 2008CPOL2 min read 109.1K   3.1K   80   12
This class helps you to monitor files. You are notified if a file is modified outside the program.
Sample Image - FileWatch.jpg

Introduction

This class helps you to monitor files like in the DevStudio. If the file is modified outside your application, a message box pops up and lets you choose between ignoring modifications or reloading the data, thus discarding all changes.

If the file is modified, a message is sent either to the specified view or to the first view of a specified document. The message handler will call the reload function of the document class. The class should be thread safe.

Things you have to change in your View Class and Document Class:

In Your Document Class Header File

C++
class CFileWatchAppDoc : public CRichEditDoc
{
    ....
public:
    void OnFileReload();
protected:
    DWORD m_hFileWatch;
    void AddToFileWatch();
};

In Your Document Class Source File

C++
#include "FileWatch.h"

CFileWatchAppDoc::CFileWatchAppDoc()
{
    m_hFileWatch = NULL;
}

CFileWatchAppDoc::~CFileWatchAppDoc()
{
    CFileWatch::RemoveHandle(m_hFileWatch);
}

BOOL CFileWatchAppDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
    CFileWatch::RemoveHandle(m_hFileWatch);
    BOOL bSuccess = CRichEditDoc::OnSaveDocument(lpszPathName);
    AddToFileWatch();
   return bSuccess;
}

void CFileWatchAppDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
{
    CFileWatch::RemoveHandle(m_hFileWatch);
    CRichEditDoc::SetPathName(lpszPathName, bAddToMRU);
    AddToFileWatch();
}

void CFileWatchAppDoc::AddToFileWatch()
{
    m_hFileWatch =  CFileWatch::AddFileFolder(lpszPathName,
                                               NULL, this);
    CFileWatch::SetAutoReload(m_hFileWatch,
        &((CYourApp*)AfxGetApp())->m_bAutoReloadDocTypeXY);
}

void CFileWatchAppDoc::OnFileReload()
{
    // your reload code
    ...
    UpdateAllViews(NULL);
}

In Your View Class Header File

C++
class CFileWatchAppView : public CRichEditView
{
    ...
protected:
    //{{AFX_MSG(CFileWatchAppView)
    afx_msg LRESULT OnFileWatchNotification(WPARAM wParam,
                                            LPARAM lParam);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

In Your View Class Source File

C++
#include "FileWatch.h"

BEGIN_MESSAGE_MAP(CFileWatchAppView, CRichEditView)
  //{{AFX_MSG_MAP(CFileWatchAppView)
  ON_REGISTERED_MESSAGE(CFileWatch::m_msgFileWatchNotify,
                        OnFileWatchNotification)
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

LRESULT CFileWatchAppView::OnFileWatchNotification(WPARAM wParam,
                                                   LPARAM lParam)
{
    CString sPathName = CString((LPCTSTR)lParam);
    DWORD hFileWatch = (DWORD)wParam;

    if (CFileWatch::Dialog(hFileWatch))
        GetDocument()->OnFileReload();

    return 0;
}

In Your Mainframe Class Source File

C++
#include "FileWatch.h"

void CMainFrame::OnClose()
{
    CFileWatch::Stop();

    CMDIFrameWnd::OnClose();
}

Add Non-document Files to the Watch

Non-document files, which should be monitored, can be added to the watch but must be associated with a view class. Use DWORD dwData or the file name to distinguish which file has been changed.

C++
#include "FileWatch.h"

void class::xy()
{
   ...
   DWORD hHandle = CFileWatch::AddFileFolder(LPCTSTR lpszFileName,
                HWND hWnd, CDocument* pDocument=NULL, DWORD dwData);
   ...
}

LRESULT CYourView::OnFileWatchNotification(WPARAM wParam,
                                                   LPARAM lParam)
{
    CString sPathName = CString((LPCTSTR)lParam);
    DWORD dwData = (DWORD)wParam;

    if (sPathName == GetDocument()->GetPathName())
      GetDocument()->OnFileReload();
    else
    ...

    return 0;
}

Note

Not all file systems record write time in the same manner. For example, on Windows NT FAT, create time has a resolution of 10 milliseconds, write time has a resolution of 2 seconds, and access time has a resolution of 1 day (really, the access date). On NTFS, access time has a resolution of 1 hour. Furthermore, FAT records times on disk in local time, while NTFS records times on disk in UTC, so it is not affected by changes in time zone or daylight saving time. I have no information about the resolution of the write time in FAT or FAT32. However, if the file is modified several times in the same time window, only one notification can be detected!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRecent article questions Pin
Vitali Halershtein9-Apr-08 0:45
Vitali Halershtein9-Apr-08 0:45 
QuestionHow about files on a Windows network? Pin
Sharpmike24-Jan-05 4:33
Sharpmike24-Jan-05 4:33 
AnswerRe: How about files on a Windows network? Pin
HQH7-Apr-08 20:02
HQH7-Apr-08 20:02 
GeneralHelp Me Please Pin
LOSTTWARE.com10-Nov-03 18:54
LOSTTWARE.com10-Nov-03 18:54 
QuestionHow we know which anothers application also are openning the file that our application is openning??? Pin
doxuanhuyen9-Oct-03 17:11
doxuanhuyen9-Oct-03 17:11 
QuestionHow to monitor directory on Linux / UNIX without polling? Pin
Mark Fine9-May-03 4:25
Mark Fine9-May-03 4:25 
AnswerRe: How to monitor directory on Linux / UNIX without polling? Pin
TheJosh2-Apr-08 13:11
TheJosh2-Apr-08 13:11 
GeneralTop Code! Pin
igorss5-May-03 22:57
igorss5-May-03 22:57 
Generalbug will happen Pin
28-Dec-02 18:48
suss28-Dec-02 18:48 
QuestionHow to use CFileWatch in the dialog-based app? Pin
Golden Lee11-Sep-02 18:38
Golden Lee11-Sep-02 18:38 
General[Message Deleted] Pin
qweqwe15-Jun-01 8:01
qweqwe15-Jun-01 8:01 
GeneralRe: DOWNLOAD LATEST VERSION HERE Pin
Anthony_Yio13-Aug-02 1:44
Anthony_Yio13-Aug-02 1:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.