Reputation: 2226
I want to track changes for files inside a directory, and notify web services whenever they change.
On a Mac, I know I can react to file system events, like creating it, modifying it, etc.
Which would be the best way to do this in Windows? Which language do you advise for this? (I will be building a basic UI for it).
Upvotes: 3
Views: 6866
Reputation: 2668
With Java we can Use the WatchService to detect the kind of events have occurred in a specific file or even directory.
The Implementation is easy as You can just get WatchService from FileSystem as follow:
1- Get the WatchService: WatchService watchService = FileSystems.getDefault().newWatchService();
2- Use the WatchService in Your file:
FILE_LOCATION.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_CREATE)
After all, You can see all kind of events that had occurred in Your file like above: StandardWatchEventKinds.ENTRY_MODIFY which mean that your file has been modified.
For more details check the link below:
Upvotes: 0
Reputation: 31296
C# has a class System.IO.FileSystemWatcher
which will probably do what you want. More details on the MDSN. For C++, see this StackOverflow question.
Upvotes: 4
Reputation: 54128
The native (C++/C) API is ReadDirectoryChangesW. In .Net (C# etc) this is encapsulated in the FileSystemWatcher class. C# is much easier to program for, there is some sample code at that URL.
These APIs are both subject to occasional missed events. There's no solution to this that I am aware of, not any easy alternative. If your directory is not changing that often, it likely will work just fine for you.
Upvotes: 4