[C++]使用ReadDirectoryChangesW API監控檔案系統的改變
hDirectoryHandle = ::CreateFileA(
file,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS
| FILE_FLAG_OVERLAPPED,
NULL);
if(hDirectoryHandle == INVALID_HANDLE_VALUE) return;
memset(buffer, 0, nBufferSize);
if(!::ReadDirectoryChangesW(
hDirectoryHandle,
buffer,
nBufferSize,
bIncludeSubdirectories,
FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_FILE_NAME,
&dwBytes,
NULL,
NULL) || GetLastError() == ERROR_INVALID_HANDLE)
{
break;
}
if(!dwBytes) { printf(“Buffer overflow~~ “); }
do { switch (record->Action) { case FILE_ACTION_ADDED: printf(“FILE_ACTION_ADDED:”); break; case FILE_ACTION_REMOVED: printf(“FILE_ACTION_REMOVED:”); break; case FILE_ACTION_MODIFIED: printf(“FILE_ACTION_MODIFIED:”); break; case FILE_ACTION_RENAMED_OLD_NAME: printf(“FILE_ACTION_RENAMED_OLD_NAME:”); break;
case FILE_ACTION_RENAMED_NEW_NAME:
printf("FILE_ACTION_RENAMED_NEW_NAME:");
break;
default:
break;
}
char fileBuffer[512];
WideCharToMultiByte(CP_ACP, 0, record->FileName, record->FileNameLength, fileBuffer, record->FileNameLength, NULL, NULL);
printf(fileBuffer);
printf("
“);
cbOffset = record->NextEntryOffset;
record = (PFILE_NOTIFY_INFORMATION)((LPBYTE) record + cbOffset);
}while(cbOffset);
#include “stdafx.h” #include “Windows.h”
void MonitorDir(char* file, bool bIncludeSubdirectories = false) { int nBufferSize = 1024; char* buffer = new char[nBufferSize]; HANDLE hDirectoryHandle = NULL;
hDirectoryHandle = ::CreateFileA(
file,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS
| FILE_FLAG_OVERLAPPED,
NULL);
if(hDirectoryHandle == INVALID_HANDLE_VALUE)
return;
while(1)
{
DWORD dwBytes = 0;
memset(buffer, 0, nBufferSize);
if(!::ReadDirectoryChangesW(
hDirectoryHandle,
buffer,
nBufferSize,
bIncludeSubdirectories,
FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_FILE_NAME,
&dwBytes,
NULL,
NULL) || GetLastError() == ERROR_INVALID_HANDLE)
{
break;
}
if(!dwBytes)
{
printf("Buffer overflow~~
“); }
PFILE_NOTIFY_INFORMATION record = (PFILE_NOTIFY_INFORMATION)buffer;
DWORD cbOffset = 0;
do
{
switch (record->Action)
{
case FILE_ACTION_ADDED:
printf("FILE_ACTION_ADDED:");
break;
case FILE_ACTION_REMOVED:
printf("FILE_ACTION_REMOVED:");
break;
case FILE_ACTION_MODIFIED:
printf("FILE_ACTION_MODIFIED:");
break;
case FILE_ACTION_RENAMED_OLD_NAME:
printf("FILE_ACTION_RENAMED_OLD_NAME:");
break;
case FILE_ACTION_RENAMED_NEW_NAME:
printf("FILE_ACTION_RENAMED_NEW_NAME:");
break;
default:
break;
}
char fileBuffer[512];
WideCharToMultiByte(CP_ACP, 0, record->FileName, record->FileNameLength, fileBuffer, record->FileNameLength, NULL, NULL);
printf(fileBuffer);
printf("
“);
cbOffset = record->NextEntryOffset;
record = (PFILE_NOTIFY_INFORMATION)((LPBYTE) record + cbOffset);
}while(cbOffset);
}
delete buffer;
if(hDirectoryHandle)
CloseHandle(hDirectoryHandle);
}
int _tmain(int argc, _TCHAR* argv[]) { MonitorDir(“C:\Users\larry\Desktop\新增資料夾”); return 0; }