Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone have a FileSystemWatcher-like class in C++/WinAPI?

I need a .Net's FileSystemWatcher analog in raw C++/WinAPI. I almost started to code one myself using FindFirstChangeNotification/FindNextChangeNotification, but then it occurred to me that I am probably not the first one who needs this and maybe someone will be willing to share.

Ideally what I need is a class which can be used as follows:

FileWatcher fw;
fw.startWatching("C:\MYDIR", "filename.dat", 
     FileWatcher::SIZE | FileWatcher::LAST_WRITE,
     &myChangeHandler);
...
fw.stopWatching();

Or if it would use somehting like boost::signal it would be even better. But please, no dependencies other than the Standard Library, boost and raw WinAPI. Thanks!

like image 559
Alex Jenter Avatar asked Jan 21 '10 06:01

Alex Jenter


People also ask

What is FileSystemWatcher in C#?

Use FileSystemWatcher to watch for changes in a specified directory. You can watch for changes in files and subdirectories of the specified directory. You can create a component to watch files on a local computer, a network drive, or a remote computer.

How does FileSystemWatcher work?

The FileSystemWatcher class in the System.IO namespace can be used to monitor changes to the file system. It watches a file or a directory in your system for changes and triggers events when changes occur. In order for the FileSystemWatcher to work, you should specify a directory that needs to be monitored.

What is a Filewatcher?

File Watcher is an IntelliJ IDEA tool that allows you to automatically run a command-line tool like compilers, formatters, or linters when you change or save a file in the IDE.

How do I monitor a file in C#?

The option to monitor files with specific extensions can be set using the Filter property of the FileSystemWatcher class. You can also fine-tune FileSystemWatcher to monitor any change in file Attributes, LastAccess, LastWrite, Security, and Size data. The FileSystemWatcher class raises the events described in Table 1.


2 Answers

What about the ReadDirectoryChangesW function?

http://msdn.microsoft.com/en-us/library/aa365465(VS.85).aspx

It stores notifications in a buffer so you don't miss any changes (unless the buffer overflows)

like image 61
Dynite Avatar answered Oct 05 '22 22:10

Dynite


There is some public-domain code here. My current project uses this (inherited from previous developers). It works pretty well but we do miss notifications for reasons that are unclear (and possibly not caused by this code).

Note that the Win32 API here has some limitations which make it difficult/impossible to avoid missing notifications. Background and alleged work-round for the API are here

like image 36
Steve Townsend Avatar answered Oct 05 '22 23:10

Steve Townsend