views:

271

answers:

1

Hi,

I'm writing some sort of backup tool that has to copy all the files in a directory. Now I'm using C#'s File.Copy(String, String, Boolean) method. But another application (which I can't change) simultaneously writes to the files in that directory.

So now I wonder if it is possible that a file gets changed halfway the copying process creating a corrupt backup file?

Thanks

+3  A: 

No, it is not. Underlying file system will make sure this is not happening.

Rashack
Does anyone has an online source that confirms and further explains this? I've tried to google it myself but I couldn't find anything decent.
J W
Are you actually experiencing the described issue? Here's a decent description: http://en.wikipedia.org/wiki/File_locking#In_Microsoft_Windows I would say it depends on how did you open your file. I bet that FileInfo.CopyTo should work more than well for you.
Rashack
No, I didn't experience the described issue. But the backup tool will be used very intensively in a production environment so I'm very careful. I don't have any control of how the other applications open the files I'm backing up. And I noticed that the File.Copy method also works when another application has the write-lock on a file (but not the read-lock). So I feared that it might make a copy halfway a change in the file, thus corrupting the file. I would be more at ease if I could find a source confirming this could not happen.
J W
Now that seems strange to me. I don't have any further knowledge to decide here.... You can check the modified date before and after copy, or do a double check by computing some CRC or ... I don't know. Sorry.
Rashack