开发者

How can I detect changes to files from Windows?

I want to track changes for files inside a directory, and notify web services whenever 开发者_JS百科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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜