Rakesh J
Rakesh J

Reputation: 41

How to copy files from remote windows machines to another windows machine using C#?

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.

Upvotes: 1

Views: 191

Answers (2)

Ron Beyer
Ron Beyer

Reputation: 11273

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.

Upvotes: 0

LawfulEvil
LawfulEvil

Reputation: 2337

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

Upvotes: 2

Related Questions