VC++文件监控 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     DWORD dwAttrib    = GetFileAttributes( strPath );

 18     return static_cast<bool>( ( dwAttrib != 0xffffffff 

 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:\\同步目录\\");

 28     WCHAR  szFileName[MAX_PATH];

 29     char*  szFilePath;

 30 

 31     PFILE_NOTIFY_INFORMATION pNotify = (PFILE_NOTIFY_INFORMATION)pBuffer;

 32 

 33     hDir = CreateFile( lpPath, FILE_LIST_DIRECTORY,

 34         FILE_SHARE_READ | 

 35         FILE_SHARE_WRITE | 

 36         FILE_SHARE_DELETE, NULL, 

 37         OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | 

 38         FILE_FLAG_OVERLAPPED, 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_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_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 (pNotify && pNotify->NextEntryOffset != 0UL)

 91                 {

 92                     PFILE_NOTIFY_INFORMATION pNext = (PFILE_NOTIFY_INFORMATION) ((LPBYTE)pNotify + pNotify->NextEntryOffset);

 93                     assert((DWORD)((BYTE*)pNext - pBuffer) < dwBufferSize);

 94                     {

 95                         WCHAR  szFileName2[MAX_PATH];

 96                         char*  szFilePath2;

 97                         memset(szFileName2, 0, MAX_PATH);

 98                         memcpy(szFileName2, lpPath, _tcslen(lpPath) * sizeof(WCHAR));

 99                         memcpy(szFileName2 + _tcslen(lpPath), pNext->FileName, pNext->FileNameLength);

100                         szFilePath2 = WideCharToMultiByte(szFileName2);

101                         if (IsDirectory(szFileName2))

102                         {

103                             printf("Directory name changed from %s to %s.\n", szFilePath, szFilePath2);

104                         }

105                         else

106                         {

107                             printf("File name changed from %s to %s.\n", szFilePath, szFilePath2);

108                         }

109                     }

110                 }

111                 break;

112             case FILE_ACTION_RENAMED_NEW_NAME:

113                 printf("FILE_ACTION_RENAMED_NEW_NAME.\n");

114                 break;

115             default:

116                 break;

117             }

118         }

119     }

120 }

关于ReadDirectoryChangesW的详细介绍可以查看
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465(v=vs.85).aspx

 

你可能感兴趣的:(Directory)