1

I have a folder in which log files are created continuously through a file simulator. On completion of creation of each file, a background windows service which is a FolderWatcher, should copy each file to a central windows server.

I have used File.Copy() to transfer the files from one machine to another. The issue is my background windows service is not able to know whether the file to be copied is still in use.

Is there any way to know that the file is still in use? I know if we try to open the file which is in use, it will throw exception.

lokusking
  • 7,396
  • 13
  • 38
  • 57
Rakesh J
  • 41
  • 6
  • 2
    No, there are no reliable ways to check if your next attempt at a file lock will succeed. Better to ask forgiveness than permission: just try opening the file and wrap in a try/catch block that retries (or errors out). – Asad Saeeduddin Apr 16 '15 at 19:37

2 Answers2

2

Catch the exception and put the file in a list of file to 'try again' on at a later time.

LawfulEvil
  • 2,267
  • 24
  • 46
0

I would check if the file is in use first, then if it is, add it to a queue that is periodically checked and move the file when its free.

See this stackoverflow question to determine how to see if a file is already open.

Community
  • 1
  • 1
Ron Beyer
  • 11,003
  • 1
  • 19
  • 37