Click here to Skip to main content
11,705,208 members (52,191 online)
Click here to Skip to main content


, 1 Apr 2008 CPOL 87.1K 2.9K 79
Rate this:
Please Sign up or sign in to vote.
This class helps you to monitor files. You are notified if a file is modified outside the program.
Sample Image - FileWatch.jpg


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

class CFileWatchAppDoc : public CRichEditDoc
    void OnFileReload();
    DWORD m_hFileWatch;
    void AddToFileWatch();

In Your Document Class Source File

#include <span class="code-string">"FileWatch.h"</span>

    m_hFileWatch = NULL;


BOOL CFileWatchAppDoc::OnSaveDocument(LPCTSTR lpszPathName)
    BOOL bSuccess = CRichEditDoc::OnSaveDocument(lpszPathName);
   return bSuccess;

void CFileWatchAppDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
    CRichEditDoc::SetPathName(lpszPathName, bAddToMRU);

void CFileWatchAppDoc::AddToFileWatch()
    m_hFileWatch =  CFileWatch::AddFileFolder(lpszPathName,
                                               NULL, this);

void CFileWatchAppDoc::OnFileReload()
    // your reload code

In Your View Class Header File

class CFileWatchAppView : public CRichEditView
    afx_msg LRESULT OnFileWatchNotification(WPARAM wParam,
                                            LPARAM lParam);

In Your View Class Source File

#include <span class="code-string">"FileWatch.h"</span>

BEGIN_MESSAGE_MAP(CFileWatchAppView, CRichEditView)

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

    if (CFileWatch::Dialog(hFileWatch))

    return 0;

In Your Mainframe Class Source File

#include <span class="code-string">"FileWatch.h"</span>

void CMainFrame::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.

#include <span class="code-string">"FileWatch.h"</span>

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())

    return 0;


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!


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


About the Author

Software Developer (Senior)
Austria Austria
No Biography provided

You may also be interested in...

Comments and Discussions

Generalsome wrong on 2tb disk Pin
nhchmg15-Dec-10 22:50
membernhchmg15-Dec-10 22:50 
GeneralRecent article questions Pin
Vitali Halershtein9-Apr-08 0:45
memberVitali Halershtein9-Apr-08 0:45 
QuestionHow about files on a Windows network? Pin
Sharpmike24-Jan-05 4:33
memberSharpmike24-Jan-05 4:33 
AnswerRe: How about files on a Windows network? Pin
HQH7-Apr-08 20:02
memberHQH7-Apr-08 20:02 
GeneralHelp Me Please Pin
LOSTTWARE.com10-Nov-03 18:54
memberLOSTTWARE.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
memberdoxuanhuyen9-Oct-03 17:11 
QuestionHow to monitor directory on Linux / UNIX without polling? Pin
Mark Fine9-May-03 4:25
memberMark Fine9-May-03 4:25 
AnswerRe: How to monitor directory on Linux / UNIX without polling? Pin
TheJosh2-Apr-08 13:11
memberTheJosh2-Apr-08 13:11 
GeneralTop Code! Pin
igorss5-May-03 22:57
memberigorss5-May-03 22:57 
Generalbug will happen Pin
hiro028-Dec-02 18:48
memberhiro028-Dec-02 18:48 
QuestionHow to use CFileWatch in the dialog-based app? Pin
Golden Lee11-Sep-02 18:38
sussGolden Lee11-Sep-02 18:38 
General[Message Deleted] Pin
qweqwe15-Jun-01 8:01
memberqweqwe15-Jun-01 8:01 
Anthony_Yio13-Aug-02 1:44
memberAnthony_Yio13-Aug-02 1:44 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 1 Apr 2008
Article Copyright 2001 by qweqwe
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid