65.9K
CodeProject 正在变化。 阅读更多。
Home

CFileWatch

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.68/5 (9投票s)

2001年6月13日

CPOL

2分钟阅读

viewsIcon

110440

downloadIcon

3116

这个类帮助你监控文件。如果文件在程序外部被修改,你会收到通知。

Sample Image - FileWatch.jpg

引言

这个类帮助你像在 DevStudio 中一样监控文件。如果文件在你的应用程序外部被修改,会弹出一个消息框,让你选择忽略修改或重新加载数据,从而丢弃所有更改。

如果文件被修改,消息会发送到指定的视图或指定文档的第一个视图。消息处理程序将调用文档类的重新加载函数。该类应该是线程安全的。

你需要在你的 View 类和 Document 类中进行更改

在你的文档类头文件中

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

在你的文档类源文件中

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

在你的视图类头文件中

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

在你的视图类源文件中

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

在你的主框架类源文件中

#include "FileWatch.h"

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

    CMDIFrameWnd::OnClose();
}

将非文档文件添加到监控中

应该被监控的非文档文件可以添加到监控中,但必须与一个视图类关联。使用 DWORD dwData 或文件名来区分哪个文件被更改。

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

注意

并非所有文件系统都以相同的方式记录写入时间。例如,在 Windows NT FAT 上,创建时间的分辨率为 10 毫秒,写入时间的分辨率为 2 秒,访问时间的分辨率为 1 天(实际上是访问日期)。在 NTFS 上,访问时间的分辨率为 1 小时。此外,FAT 在磁盘上以本地时间记录时间,而 NTFS 在磁盘上以 UTC 记录时间,因此不受时区或夏令时更改的影响。我没有关于 FAT 或 FAT32 中写入时间分辨率的信息。但是,如果文件在相同的时间窗口内被修改多次,则只能检测到一个通知!

© . All rights reserved.