SHARE
Facebook X Pinterest WhatsApp

CFileWatch

Environment: VC6 SP4 Win2000 This class helps you to monitor files like in the DevStudio. If the file is modified outside, a message box pops up and lets you choose between ignoring modifications or reloading the data, discarding all changes. If the file is modified, a message is send either to the specified view or […]

Written By
thumbnail
CodeGuru Staff
CodeGuru Staff
Jun 6, 2001
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: VC6 SP4 Win2000

This class helps you to monitor files like in the DevStudio. If the file is
modified outside, a message box pops up and lets you choose between ignoring
modifications or reloading the data, discarding all changes.
If the file is modified, a message is send 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 save.

Things you have to change in your ViewClass and DocumentClass:

In your document class header file:

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

In your document class source file:

#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:

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:

#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 dwData = (DWORD)wParam;

   GetDocument()->OnFileReload();

    return 0;
}

In your mainframe class source file:

#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.

#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;
}

ATTENTION!

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 one
day (really, the access date). On NTFS, access time has a resolution of one
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!!!

Download latest version here.

Downloads

Download demo project – 60 Kb
Download source – 3 Kb

Recommended for you...

How To Make Windows 11 Faster
Enrique Stone
Nov 30, 2022
Working with the Windows Registry in C#
Joydip Kanjilal
Jun 17, 2022
Using Multiple Programming Languages to Create an ASP.NET Website
Tariq Siddiqui
Jun 11, 2022
Finding a Microsoft Office version with .NET
Hannes DuPreez
May 20, 2022
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.