流逝时间+Windows下监测文件夹

查看流逝时间:

1 #include <time.h>
2 #include <iostream>
3 
4 clock_t startClock = clock();
5 clock_t endClock = clock();
6 cout << "耗时:" << endClock - startClock << ", 单位:" << CLOCK_PER_SEC << endl;

Windows下监测文件夹:

  1 char* WideCharToMultiByte(LPCTSTR widestr)
  2 {
  3     int num = WideCharToMultiByte(CP_OEMCP, NULL, widestr, -1, NULL, 0, NULL, FALSE);
  4     char *pchar = new char[num];
  5     WideCharToMultiByte(CP_OEMCP, NULL, widestr, -1, pchar, num, NULL, FALSE);
  6     return pchar;
  7 }
  8 
  9 bool IsDirectory(const LPTSTR & strPath)
 10 {
 11     DWORD dwAttrib = GetFileAttributes(strPath);
 12     return static_cast<bool>((dwAttrib != 0xffffffff
 13         && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)));
 14 }
 15 
 16 void Listen(const char *szStr)
 17 {
 18     //监听文件
 19     WCHAR wszClassName[256];
 20     memset(wszClassName, 0, sizeof(wszClassName));
 21     MultiByteToWideChar(CP_ACP, 0, szStr, strlen(szStr) + 1, wszClassName, sizeof(wszClassName) / sizeof(wszClassName[0]));
 22 
 23 
 24     HANDLE hDir;
 25     BYTE*  pBuffer = (LPBYTE)new CHAR[4096];
 26     DWORD  dwBufferSize;
 27 
 28     WCHAR  szFileName[MAX_PATH];
 29     char*  szFilePath;
 30 
 31     PFILE_NOTIFY_INFORMATION pNotify = (PFILE_NOTIFY_INFORMATION)pBuffer;
 32 
 33     hDir = CreateFile(wszClassName, FILE_LIST_DIRECTORY,
 34         FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
 35         NULL,
 36         OPEN_EXISTING,
 37         FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
 38         NULL);
 39 
 40 
 41     if (hDir == INVALID_HANDLE_VALUE)
 42     {
 43         return;
 44     }
 45 
 46     while (TRUE)
 47     {
 48         if (ReadDirectoryChangesW(hDir,
 49             pBuffer,
 50             4096,
 51             TRUE,
 52             FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE,
 53             &dwBufferSize,
 54             NULL,
 55             NULL))
 56         {
 57             memset(szFileName, 0, MAX_PATH);
 58             memcpy(szFileName, wszClassName, wcslen(wszClassName) * sizeof(WCHAR));
 59             memcpy(szFileName + wcslen(wszClassName), pNotify->FileName, pNotify->FileNameLength);
 60             szFilePath = WideCharToMultiByte(szFileName);
 61 
 62             switch (pNotify->Action)
 63             {
 64             case FILE_ACTION_ADDED:
 65                 if (IsDirectory(szFileName))
 66                 {
 67                     printf("Directory %s added.\n", szFilePath);
 68                 }
 69                 else
 70                 {
 71                     printf("File %s added.\n", szFilePath);
 72                 }
 73                 break;
 74             case FILE_ACTION_REMOVED:
 75                 if (IsDirectory(szFileName))
 76                 {
 77                     printf("Directory %s removed.\n", szFilePath);
 78                 }
 79                 else
 80                 {
 81                     printf("File %s removed.\n", szFilePath);
 82                 }
 83                 break;
 84             case FILE_ACTION_MODIFIED:
 85                 if (!IsDirectory(szFileName))
 86                 {
 87                     printf("File %s modified.\n", szFilePath);
 88                 }
 89                 break;
 90             case FILE_ACTION_RENAMED_OLD_NAME:
 91                 if (pNotify && pNotify->NextEntryOffset != 0UL)
 92                 {
 93                     PFILE_NOTIFY_INFORMATION pNext = (PFILE_NOTIFY_INFORMATION)((LPBYTE)pNotify + pNotify->NextEntryOffset);
 94                     //assert((DWORD)((BYTE*)pNext - pBuffer) < dwBufferSize);
 95                     {
 96                         WCHAR  szFileName2[MAX_PATH];
 97                         char*  szFilePath2;
 98                         memset(szFileName2, 0, MAX_PATH);
 99                         memcpy(szFileName2, wszClassName, wcslen(wszClassName) * sizeof(WCHAR));
100                         memcpy(szFileName2 + wcslen(wszClassName), pNext->FileName, pNext->FileNameLength);
101                         szFilePath2 = WideCharToMultiByte(szFileName2);
102                         if (IsDirectory(szFileName2))
103                         {
104                             printf("Directory name changed from %s to %s.\n", szFilePath, szFilePath2);
105                         }
106                         else
107                         {
108                             printf("File name changed from %s to %s.\n", szFilePath, szFilePath2);
109                         }
110                     }
111                 }
112                 break;
113             case FILE_ACTION_RENAMED_NEW_NAME:
114                 printf("FILE_ACTION_RENAMED_NEW_NAME.\n");
115                 break;
116             default:
117                 break;
118             }
119         }
120     }
121 }

 

posted @ 2021-11-17 22:10  wisdomroc  阅读(50)  评论(0编辑  收藏  举报