Filewatcher for the whole computer (alternative?)

2019-08-15 03:34发布

问题:

I want to write an application that gets events on every file change on the whole computer (to synchronize between file locations/permissions and my application's database).

I was thinking of using the .net filewatcher class but after some tests i found the following limitations:

1) The filewatcher has a buffer (http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.90).aspx):

If there are many changes in a short time, the buffer can overflow. This causes the component to lose track of changes in the directory, and it will only provide blanket notification. Increasing the size of the buffer with the InternalBufferSize property is expensive, as it comes from non-paged memory that cannot be swapped out to disk, so keep the buffer as small yet large enough to not miss any file change events. To avoid a buffer overflow, use the NotifyFilter and IncludeSubdirectories properties so you can filter out unwanted change notifications.

So in the whole computer, I can get some large amount of events (in peak) that i need to handle. Even if inside the event handling I'm only adding the event info to a queue I still can miss events.

2) Filewatcher has memory leaks: http://connect.microsoft.com/VisualStudio/feedback/details/654232/filesystemwatcher-memory-leak I checked it myself and it's true, after a few days my process memory grows from 20MB to 250MB

3) Microsoft says that we should use filewatcher for specific folders (I don't know why):

Use FileSystemWatcher to watch for changes in a specified directory.

So for these reasons i need an alternative solution to create my application. I know that I can write a driver but I prefer it to be a .net solution (based on win32 api, of course).

Thank you for your help, Omri

回答1:

Putting monitoring (especially synchronous notifications) will slow down the system. You can probably make use of our CallbackFilter product which provides a driver and a handy .NET API for tracking file changes. And CallbackFilter supports asynchronous notifications which are faster. Discounted and free licenses are possible.



回答2:

Try doing this through WMI imo - the following link is relevant: http://www.codeproject.com/Articles/42212/WMI-and-File-System-Monitoring