Does FileSystemWatcher work?

I install FsWatcher in a local file system directory. I only want to know when files are added to the directory so that they can be moved to another file system. It seems I can detect when the first file is located, but actually I want to know when all the files from this copy operation are completed.

If I used Windows Explorer to copy files from one directory to another, Explorer will tell me that there are n seconds remaining in the transfer, so although there are some steps to start transferring and transferring the end for each file, there seems to be something to start -Transfer and end transfer for all files.

I wonder if there is something like this that I can only do with the .NET Framework. I would like to know when "all" files are located, and not just one file in a "transaction". If nothing is baked, maybe I should come up with some kind of expectation / opposition in order to carry out my activity only when the work is β€œdone”.

Not sure if I make 100% sense on this, please comment.

Thanks.

+2
source share
3 answers

The way I've implemented this in the past is to have a marker file with a special name, for example. 'end'. This file is always the last recorded file and serves as an indicator of when the transaction of several files is completed. This will work just fine if there is only one thread that writes these sets of files.

If there are many threads, each of which can write a set of files, then you need to somehow associate each "final" file with the set of files that it marks. One way to do this is to include a GUID (or other unique identifier) ​​in the file names, for example.

{GUID1.file1.bin, GUID1.file2.bin, GUID1.end}

{GUID2.file1.bin, GUID2.file2.bin, GUID2.end}

Another way to do this is to consolidate all the files from each set into one ZIP file so that you can watch only individual files.

+3
source

There is no such thing as a transaction for a copy of a file. You can only watch individual changes to files in a directory.

Explorer can do this because it knows what you want to do (copy these x files here here).

You do not even know when this file is written (or, in a more general sense, modified). You need to try to lock the file exclusively so that you know that you are its only one.

The .end tag of the file that AdamRalph mentions is the only trick that actually comes close to what you want to do.

Hth

Mario

+1
source

As for writing a file with a list of files to be added as a first step, your application will pick it up, read it, and then wait for the files to be written. When the final file is written, you can pack them and send them. Thus, if several "transactions" are triggered, you do not have "target files" advancing on each other. Give the indicator file a well-known template name, which is unlikely to happen in regular files, and will provide some means so that several indicator files can appear at the same time.

If I were to implement something like this, I would probably use some kind of file name, for example ~<ANOTHER-UNIQUE-GUID>.tmp , my file system observer will keep track of a single file name of this format, which in turn tells the observer of another file system to wait on the files listed inside. When the files have been deleted, the temporary file can be deleted.

This approach can be easily cut so that each of your observers does not step over each other and does not transfer / delete files that other observers are working on.

As suggested earlier, it may be more efficient to write files to a single archive and use them by unpacking them at the destination.

0
source

All Articles