VC ++ file monitoring ReadDirectoryChangesW,
1 # include <windows. h> 2 # include <tchar. h> 3 # include <stdio. h> 4 5 # include <assert. h> 6 7 char * WideCharToMultiByte (LPCTSTR widestr) 8 {9 int num = WideCharToMultiByte (CP_OEMCP, NULL, widestr,-1, NULL, 0, NULL, FALSE ); 10 char * pchar = new char [num]; 11 WideCharToMultiByte (CP_OEMCP, NULL, widestr,-1, pchar, num, NULL, FALSE); 12 return pchar; 13} 14 15 bool IsDirectory (const LPTSTR & strPath) 16 {17 DWO RD dwAttrib = GetFileAttributes (strPath); 18 return static_cast <bool> (dwAttrib! = 0 xffffffff 19 & (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); 20} 21 22 int main () 23 {24 HANDLE hDir; 25 BYTE * pBuffer = (LPBYTE) new CHAR [4096]; 26 DWORD dwBufferSize; 27 LPTSTR lpPath = _ T ("D: \ synchronization directory \"); 28 WCHAR szFileName [MAX_PATH]; 29 char * szFilePath; 30 31 pfile_policy_information pNotify = (pfile_policy_information) pBuffer; 32 33 hDir = CreateFile (lpPath, FILE_LIST_DIRECTORY, 34 FILE_SHAR E_READ | 35 file_cmd_write | 36 file_cmd_delete, NULL, 37 OPEN_EXISTING, bytes | 38 bytes, NULL); 39 if (hDir = INVALID_HANDLE_VALUE) 40 {41 printf ("INVALID_HANDLE_VALUE "); 42 return 0; 43} 44 45 while (TRUE) 46 {47 if (ReadDirectoryChangesW (hDir, 48 pBuffer, 49 4096, 50 TRUE, 51 file_policy_change_file_name | file_policy_change_dir_name | file_policy_cha NGE_LAST_WRITE, 52 & dwBufferSize, 53 NULL, 54 NULL) 55 {56 memset (szFileName, 0, MAX_PATH); 57 memcpy (szFileName, lpPath, _ tcslen (lpPath) * sizeof (WCHAR); 58 memcpy (szFileName + _ tcslen (lpPath), pNotify-> FileName, pNotify-> FileNameLength); 59 szFilePath = WideCharToMultiByte (szFileName ); 60 61 switch (pNotify-> Action) 62 {63 case FILE_ACTION_ADDED: 64 if (IsDirectory (szFileName) 65 {66 printf ("Directory % s added. \ n ", szFilePath); 67} 68 else 69 {70 printf (" File % s added. \ n ", szFilePath); 71} 72 break; 73 case FILE_ACTION_REMOVED: 74 if (IsDirectory (szFileName) 75 {76 printf (" Directory % s removed. \ n ", szFilePath); 77} 78 else 79 {80 printf (" File % s removed. \ n ", szFilePath); 81} 82 break; 83 case FILE_ACTION_MODIFIED: 84 if (! IsDirectory (szFileName) 85 {86 printf ("File % s modified. \ n ", szFilePath); 87} 88 break; 89 case FILE_ACTION_RENAMED_OLD_NAME: 90 if (py y & pNotify-> NextEntryOffset! = 0UL) 91 {92 pfile_policy_information pNext = (pfile_policy_information) (LPBYTE) py y + pNotify-> NextEntryOffset); 93 assert (DWORD) (BYTE *) pNext-pBuffer) <dwBufferSize); 94 {95 WCHAR szFileName2 [MAX_PATH]; 96 char * szFilePath2; 97 memset (delimiter, 0, MAX_PATH); 98 memcpy (szFileName2, lpPath, _ tcslen (lpPath) * sizeof (WCHAR); 99 memcpy (szFileName2 + _ tcslen (lpPath), pNext-> FileName, pNext-> FileNameLength); 100 szFilePath2 = listen (szFileName2 ); 101 if (IsDirectory (szFileName2) 102 {103 printf ("Directory name changed from % s to % s. \ n ", szFilePath, szFilePath2); 104} 105 else106 {107 printf (" File name changed from % s to % s. \ n ", szFilePath, szFilePath2); 108} 109} 110 break; 111 case FILE_ACTION_RENAMED_NEW_NAME: 112 printf (" FILE_ACTION_RENAMED_NEW_NAME. \ n "); 114 break; 115 default: 116 break; 117} 118} 119}
For more information about ReadDirectoryChangesW, see
Https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465 (v = vs.85). aspx