À©µµ¿ìÁî¿¡¼ ÆÄÀÏÀÌ ¼öÁ¤µÇ´Â°ÍÀ» °¨ÁöÇغ¸ÀÚ. À©µµ¿ìÁîÀÇ Æú´õ ¼öÁ¤ °¨Áö API¸¦ ÀÌ¿ëÇÏ¿© ÆÄÀÏÀÌ ¼öÁ¤µÇ´Â°ÍÀ» ¾Ë¼ö ÀÖ´Ù. FindFirstChangeNotification ÇÔ¼ö·Î Æú´õ³»¿¡ ÆÄÀÏÀÌ ¼öÁ¤µÇ´Â°ÍÀ» °¨Áö ÇÒ¼ö ÀÖ´Ù. SaveFileTime ½Ã°£ÀÌ ¹Ù²î¾úÀ¸¸é ÆÄÀÏÀÌ ¼öÁ¤µÈ °É·Î ÀνÄÇÑ´Ù. HWND hMainWnd;
FILETIME SaveFileTime; HANDLE g_hEvent = 0; void OnMyMessage() { WIN32_FIND_DATA data; HANDLE handle = FindFirstFile(L"c:\\test\\file.txt", &data); if (handle != INVALID_HANDLE_VALUE) { FindClose(handle); if (CompareFileTime(&data.ftLastWriteTime, &SaveFileTime) != 0) OutputDebugStringA("file.txt was modified\n"); else OutputDebugStringA("Another file in the same directory was modified\n"); } else { OutputDebugStringA("file.txt was deleted, or directory was removed/renamed\n"); } } DWORD WINAPI CheckFolder(void* arg) { wchar_t folder[MAX_PATH]; lstrcpy(folder, (const wchar_t*)arg); HANDLE hEvent[2] = {g_hEvent, NULL}; for (;;) { HANDLE hfolder = FindFirstChangeNotification(folder, FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_FILE_NAME); hEvent[1] = hfolder; DWORD dw = WaitForMultipleObjects(2, hEvent, FALSE, INFINITE); if (!::IsWindow(hMainWnd)) break; if(dw == WAIT_OBJECT_0) { OutputDebugStringA("thread exit\n"); break; } else if(dw == (WAIT_OBJECT_0+1)) { PostMessage(hMainWnd, WM_COMMAND, ID_MY_MESSAGE, 0); } FindCloseChangeNotification(hfolder); } return 0; } void TestMain() { //ÀÚµ¿À¸·Î Reset ÇØÁÖ±â À§ÇØ µÎ¹ø° ÀÎÀÚ¸¦ FALSE·Î ¼³Á¤ÇÑ´Ù. g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); //save last write time WIN32_FIND_DATA data; HANDLE h = FindFirstFile(L"c:\\test\\file.txt", &data); if (h != INVALID_HANDLE_VALUE) SaveFileTime = data.ftLastWriteTime; FindClose(h); //watch for changes CreateThread(NULL, 0, CheckFolder, L"c:\\test", 0, NULL); } WaitForMultipleObjects´Â ÇÁ·Î±×·¥¿¡¼ hEvent[0]Àº ½º·¹µå Á¾·á À̺¥Æ®, hEvent[1]Àº Æú´õ °¨½Ã À̺¥Æ®ÀÌ´Ù. ÇÁ·Î±×·¥ÀÌ Á¾·á µÉ¶§ SetEvent(g_hEvent)·Î º¸³» ½º·¹µå¸¦ Á¾·áÇÑ´Ù. À©µµ¿ì ¸Þ¼¼Áö¸¦ ó¸®ÇÏ´Â ÇÔ¼ö¿¡¼ OnMyMessage ÇÔ¼ö¸¦ ½ÇÇàÇÑ´Ù. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{ int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // ¸Þ´ºÀÇ ¼±Åà ¿µ¿ªÀ» ±¸¹® ºÐ¼®ÇÕ´Ï´Ù. switch (wmId) { case ID_MY_MESSAGE: OnMyMessage(); break; ..... case WM_DESTROY: SetEvent(g_hEvent); PostQuitMessage(0); break; ´Ù¿î·Îµå : FolderMonitor.cpp Âü°í) https://stackoverflow.com/questions/32231676/monitoring-file-update-using-c https://seobby.tistory.com/76 |