3

I am writing a tool that listens a location, preferably remote location, and if a new folder or file created, it will download it to a local location.

Currently I am listening the remote folder with FileSystemWatcher, when a new folder/file created, I start a timer and if timer reaches X minutes it starts to copy it to local. Creating a new folder or file in "watched " folder triggers FileSystemWatcher.Changed but it sometimes fail if there are a lot of sub-directories and if there is a large file copying to watched folder, it only detects it when copying started and my timer can finish until it is finished.

So: I have 3 remote computers/locations, A,B,C

A starts to copy some folders/files to B and C listens to B .

How can C check if A is finished copying with or without FileSystemWatcher? I don't want to constantly compare B and C and copy rest of the files.

I checked other questions but they don't answer or I already did implement those solutions. 1, 2,3

Cagurtay
  • 45
  • 1
  • 3
  • 12
  • If this is feasible, you could use a temporary xxx.$$$tmp file name while copying and rename to the original file name, once the copy operation is complete. You then can periodically scan for non-temp files not present on your target computer. – Axel Kemper Oct 13 '17 at 13:02
  • Just about every single question about FSW, and there are many, discuss this issue. A timer just doesn't mean anything, you have no idea what other process is busy copying the file, on what machine it runs and how long it is going to take. Keep the timer, but set its Interval to a low value, like 100 msec. And keep trying, eventually you can open the file. – Hans Passant Oct 13 '17 at 13:14
  • Is this of use : https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use – PaulF Oct 13 '17 at 13:20

1 Answers1

5

I think you are asking about the system change journal. That said, there will always be cases where the file is in use, has been deleted, updated, etc. between the time you detect you need to copy it and when you really start copying. Here's an old but accurate article that can give you more details.

http://www.microsoft.com/msj/0999/journal/journal.aspx

From the article abstract:

"The Windows 2000 Change Journal is a database that contains a list of every change made to the files or directories on an NTFS 5.0 volume. Each volume has its own Change Journal database that contains records reflecting the changes occurring to that volume's files and directories."

Scroll down to the heading ReasonMask and ReturnOnlyOnClose

No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43
  • Here's a link one can use to start browsing modern [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/hh802706%28v=vs.85%29.aspx) from the bottom after reading the article, mentioned in the answer. – Sergey.quixoticaxis.Ivanov Oct 13 '17 at 14:04
  • FileSystemWatcher is basically a wrapper class for the native API ReadDirectoryChanges so I think I would disregard this answer until that changes. Better link describing this: https://blogs.msdn.microsoft.com/winsdk/2015/05/19/filesystemwatcher-follies/ (first of 3 blog posts) – No Refunds No Returns Aug 30 '18 at 01:01
  • .Net wrapper ignores some events that are available in native API. – Sergey.quixoticaxis.Ivanov Aug 31 '18 at 06:06