Gold mine of Visual C++ tricks!
How to get the File/Directory change notifications?
![]()
Usually projects have several settings stored in configuration files and in some special cases, the configuration can change at runtime and the changes should reflect instantaneously. How it can be done? Well, it can be done with the help of a timer and reload the configuration every 5 seconds. Or is there another smarter api?
![]()
Yep! you can use – ReadDirectoryChangesW(). It can be called synchronously as well as asynchronously. If its called in synchronous the calling thread will be in blocking state and return only if any file change is detected. See the code snippet for calling it synchronously.
// Directory to watch.
CString csDirectory = _T("C:\\Config");
HANDLE hDirectory = CreateFile( csDirectory,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ|FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL );
// Buffer to get the notification information.
const int MAX_BUFFER = 1024;
BYTE Buffer[MAX_BUFFER];
DWORD dwBytesReturned = 0;
ReadDirectoryChangesW( hDirectory,
Buffer,
MAX_BUFFER,
TRUE,
FILE_NOTIFY_CHANGE_LAST_WRITE,
&dwBytesReturned,
0,
0 );
// Cast the buffer as Notification Struct.
FILE_NOTIFY_INFORMATION* pNotifyInfo =
(FILE_NOTIFY_INFORMATION*)( Buffer );
// Changed file.
CString csFileName = pNotifyInfo->FileName;
You can give watch filters via dwNotifyFilter parameter. So that you can watch for specific events such as filename change, new file creation, file size change etc.
![]()
The interesting part is – unlike other api’s, ReadDirectoryChangesW() have unicode version only. So don’t forget to add UNICODE and _UNICODE to your project settings. At first if you try to compile it, most probably you’ll hit the following error.
error C2065: 'ReadDirectoryChangesW' : undeclared identifier
For fixing this add _WIN32_WINNT=0×0400 to the preprocessor settings in C/C++ tab of project settings. I hope you understand – why.
![]()
Targeted Audience – Intermediate.
| Print article | This entry was posted by Jijo Raj on June 11, 2008 at 5:54 pm, and is filed under Codeproject, Windows APIs. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |