简体   繁体   中英

How can I detect changes to files from Windows?

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).

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.

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 .

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:

https://github.com/PascoalBayonne/Exercise/blob/master/src/nio/directory/watching/DirectoryWatching.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM