I am copying several backup files which includes small to large files that is greater that 1 TB from a local folder to a network shared folder using C# File.Copy() function. Previously this worked well. But in recent times I am facing different types of exception at different time. I also tried using the NET USE command to create a shared path connection even though the credentials are the same to resolve this problem.
File.Copy(sourceFilePath, destinationFilePath, overwrite);
The exceptions I am getting are:
– Error: Could not find file.
– Error: The handle is invalid.
– Error: There are currently no logon servers available to service the logon request.
– Error: The specified network name is no longer available.
NB: These exceptions are not for invalid file path. Because exceptions occurs after copying some portions of the file and same code was worked before for the same files.
Anyone have the idea to resolve this kind of situation?
There are known problems with Windows when copying very large files. See Windows file copy bug revisited, for example. The problem seems to be that Windows wants to cache the file, and it goes to heroic efforts to do so. It ends up allocating almost all memory to caching, finally causing fatal thrashing. This will cause a non-deterministic error on the other system (that’s trying to copy the file).
The way to counter that is by copying the file without buffering by calling
CopyFileEx. Unfortunately, there is no direct way to do that from the .NET Framework. So I wrote and published some code that does. See A Better File.Copy Replacement.