WinFileSystemMonitor.h

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 
#pragma once

// 文件目录改变的类型
enum tagACTION 

    Added     = 1,           //添加了文件/目录
    Removed   = 2,           //删除了文件/目录
    Modified  = 3,           //更改了文件/目录
    Renamed   = 4            //重命名了文件/目录
};

//定义文件/目录改变后的回调函数指针
typedef  void(*lpFunDealFile)(tagACTION action, LPCTSTR lpszFileSrc, LPCTSTR lpszFileDst);


class CWinFileSystemMonitor
{

public:
    CWinFileSystemMonitor();
    ~CWinFileSystemMonitor();

    /*
    * 设置回调函数
    */
    void SetDealFilePtr( lpFunDealFile pFunDeal );

    /*
    * 开启监视
    */
    BOOL StartMonitor( LPCTSTR lpszDir );

    /*
    * 停止监视
    */
    void EndMonitor();

    /*
    * 正在监视?
    */
    BOOL IsMoniting();
    
    /*
    * 回调函数
    */
    static void ActionProcess(tagACTION action, LPCTSTR lpszFileSrc, LPCTSTR lpszFileDst);

private:
    //监视文件/目录句柄
    HANDLE  m_hDir;

    //继续监视
    volatile BOOL m_bContinue;
    
    //监视线程句柄
    HANDLE  m_hThread;
    
    //回调指针
    lpFunDealFile m_pFunDeal;

    //监视线程处理函数
    static DWORD WINAPI MonitorProc( LPVOID lParam );

};

  WinFileSystemMonitor.cpp

 

 

 

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
 
#include "WinFileSystemMonitor.h"

CWinFileSystemMonitor::CWinFileSystemMonitor(void)
{
    m_hDir       =  NULL;
    m_bContinue  =  FALSE;
    m_hThread    =  NULL;
}

CWinFileSystemMonitor::~CWinFileSystemMonitor(void)
{
}

void CWinFileSystemMonitor::SetDealFilePtr(lpFunDealFile pFunDeal)
{
    ASSERT( pFunDeal != NULL );
    m_pFunDeal = pFunDeal;
}

BOOL CWinFileSystemMonitor::StartMonitor( LPCTSTR lpszDir )
{
    ASSERT( m_hThread == NULL );

    HANDLE hDir = ::CreateFile(lpszDir,
                               GENERIC_READ | FILE_LIST_DIRECTORY,
                               FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                               NULL,
                               OPEN_EXISTING,
                               FILE_FLAG_BACKUP_SEMANTICS,
                               NULL);
    if(INVALID_HANDLE_VALUE == hDir)
    {
        return FALSE;
    }

    m_hDir = hDir;
    m_bContinue = TRUE;
    m_hThread = ::CreateThread( NULL, 0, MonitorProc, this, 0, NULL );
    if(NULL != m_hThread)
    {
        SetDealFilePtr(ActionProcess);
    }
    return (NULL == m_hThread ) ? FALSE : TRUE;
}

void CWinFileSystemMonitor::EndMonitor()
{
    ASSERT( m_hThread != NULL );
    m_bContinue = FALSE;
    DWORD dwRet = ::WaitForSingleObject(m_hThread, 1000);
    if(WAIT_TIMEOUT == dwRet)
    {
        ASSERT( m_hThread != NULL );
        ::TerminateThread(m_hThread, -1);
    }

    ::CloseHandle(m_hDir);
    m_hDir = NULL;
    m_hThread = NULL;

}

BOOL CWinFileSystemMonitor::IsMoniting()
{
    return m_bContinue;
}

DWORD WINAPI CWinFileSystemMonitor::MonitorProc( LPVOID lParam )
{
    CWinFileSystemMonitor *pThis = (CWinFileSystemMonitor *)lParam;
    ASSERT( pThis != NULL );

    char szBuf[ 2 * ( sizeof ( FILE_NOTIFY_INFORMATION ) + MAX_PATH ) ] = {0};
    FILE_NOTIFY_INFORMATION *pNotify = (FILE_NOTIFY_INFORMATION *)szBuf;
    DWORD dwBytesReturned = 0;

    while( pThis->m_bContinue )
    {
        if( !::ReadDirectoryChangesW(pThis->m_hDir,
                                     pNotify,
                                     sizeof(szBuf),
                                     TRUE,
                                     FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME |
                                     FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE |
                                     FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_LAST_ACCESS |
                                     FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_SECURITY,
                                     &dwBytesReturned,
                                     NULL,
                                     NULL ) )
        {
            break;
        }
        else
        {
            WCHAR *pszFileDst = NULL;
            WCHAR *pszFileSrc = pNotify->FileName;
            pszFileSrc[pNotify->FileNameLength / 2] = L'\0';

            if(0 != pNotify->NextEntryOffset)
            {
                PFILE_NOTIFY_INFORMATION pNext = (PFILE_NOTIFY_INFORMATION)((char *)pNotify + pNotify->NextEntryOffset);
                pszFileDst = pNext->FileName;
                pszFileDst[pNext->FileNameLength / 2] = L'\0';
            }
            if(NULL != pThis->m_pFunDeal)
            {
                pThis->m_pFunDeal((tagACTION)pNotify->Action, CW2T(pszFileSrc), CW2T(pszFileDst));
            }
        }
    }

    return 0;
}

void CWinFileSystemMonitor::ActionProcess( tagACTION action, LPCTSTR lpszFileSrc, LPCTSTR lpszFileDst )
{
    switch (action)
    {
    case Added:
        AfxMessageBox(_T("Added!"));
        break;
    case Removed:
        AfxMessageBox(_T("Removed!"));
        break;
    case Modified:
        AfxMessageBox(_T("Modified!"));
        break;
    case Renamed:
        AfxMessageBox(_T("Renamed!"));
        break;
    default:
        break;
    }
}

  一个Demo,仅供参考,欢迎改进!

  VC++ 监视文件(夹)_c++

   百度云下载:http://pan.baidu.com/s/1c6c4eM 密码:t5a4