1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| #include <windows.h> #include <iostream> #include <string> #include <filesystem> #include <fcntl.h> #include <io.h> #include <cstdio> #include <csignal>
volatile bool running = true;
void signalHandler(int signum) { if (signum == SIGINT) { running = false; } }
void DisplayFileAction(DWORD dwAction) { switch (dwAction) { case FILE_ACTION_ADDED: std::wcout << L"File Created" << std::endl; break; case FILE_ACTION_REMOVED: std::wcout << L"File Deleted" << std::endl; break; case FILE_ACTION_MODIFIED: std::wcout << L"File Modified" << std::endl; break; case FILE_ACTION_RENAMED_OLD_NAME: std::wcout << L"File Renamed (Old Name)" << std::endl; break; case FILE_ACTION_RENAMED_NEW_NAME: std::wcout << L"File Renamed (New Name)" << std::endl; break; default: std::wcout << L"Unknown Action" << std::endl; break; } }
int main() { signal(SIGINT, signalHandler);
_setmode(_fileno(stdout), _O_U16TEXT); _setmode(_fileno(stdin), _O_U16TEXT);
std::wstring directory; std::wcout << L"Enter the directory you want to monitor: "; std::getline(std::wcin, directory); HANDLE hDir = CreateFileW( directory.c_str(), FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL );
if (hDir == INVALID_HANDLE_VALUE) { std::wcerr << L"Error: Cannot open directory " << directory << std::endl; return 1; }
std::wcout << L"Monitoring directory: " << directory << std::endl; std::wcout << L"Press Ctrl+C to exit" << std::endl;
const DWORD BUFFER_SIZE = 4096; BYTE buffer[BUFFER_SIZE] = { 0 }; DWORD bytesReturned; OVERLAPPED overlapped = { 0 }; overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
while (running) { BOOL success = ReadDirectoryChangesW( hDir, buffer, BUFFER_SIZE, TRUE, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_LAST_ACCESS | FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_SECURITY, &bytesReturned, &overlapped, NULL );
if (!success) { std::wcerr << L"ReadDirectoryChangesW failed" << std::endl; break; }
DWORD waitResult = WaitForSingleObject(overlapped.hEvent, 1000); if (waitResult == WAIT_OBJECT_0) { FILE_NOTIFY_INFORMATION* pNotify = (FILE_NOTIFY_INFORMATION*)buffer; do { std::wstring filename(pNotify->FileName, pNotify->FileNameLength / sizeof(WCHAR)); std::wcout << L"File: " << filename << L" - "; DisplayFileAction(pNotify->Action); if (pNotify->NextEntryOffset == 0) break; pNotify = (FILE_NOTIFY_INFORMATION*)((BYTE*)pNotify + pNotify->NextEntryOffset); } while (true);
ResetEvent(overlapped.hEvent); } }
std::wcout << L"\nExiting program..." << std::endl;
CloseHandle(overlapped.hEvent); CloseHandle(hDir);
return 0; }
|