System.IO.File.Copy() produces System.IO.IOException: The specified network name is no longer available

15,346

Solution 1

It turns out that the customer that was using this software was running two instances of it simultaneously, against the same data set. Once the redundant instance was stopped it resolved the error. Thanks everyone who answered.

Solution 2

File.Copy() opens up underline streams. You might have lost connection while File.Copy() is in progress. So, it can't flush and close the stream.

One possibility to recover from this, is to use the FileStream class and call Win32 API CloseHandle when such exception occurs, doing so will release the OS file handle so you can re-open the file.

[ DllImport("Kernel32") ]
public static extern bool CloseHandle(IntPtr handle);

FileStream fs;
try {
...
}

catch(IOException)
{
// If resource no longer available, or unable to write to.....
if(...)
CloseHandle(fs.Handle);
}

Also, MSDN recommends not to rely on overwrite. Try deleting existing file and creating new one when copying them.

File.Copy(..., ..., TRUE) does not work properly.

Be very careful with this method, as the Overwrite = True does NOT work properly.

I had an existing destination file that had some information inside it that was somehow preserved and carried over to the source file that was supposed to copy over it.  This should be impossible, but I confirmed it for myself.

Solution 3

The error seems to indicate that the network connection is lost partway through and probably isn't to do with the code at all. If the same folder copy succeeds sometimes and fails other times then this would back up that it's not the code to blame and must be a resource access issue.

Share:
15,346

Related videos on Youtube

JMorgan
Author by

JMorgan

Software Engineer for M*Modal

Updated on June 04, 2022

Comments

  • JMorgan
    JMorgan over 1 year

    I have a windows service (C# .Net 3.5) that grabs data from a network share and does a copy to the host of the service.

    The size of the data copied ranges from 50KB to 750MB, and the number of files copied varies. In perhaps 20% of the copies I am getting System.IO.IOException: The specified network name is no longer available.

    My google-fu is failing to turn up an answer as to what might cause this during a File.Copy. Has anyone seen/solved this before?

    Here is the recursive method that does the copy. The exception occurs on line File.Copy(fromFile, toFile, overwrite);

    private static int RecursiveCopyDirectory(string from, string to, bool merge, bool overwrite, int depth)
        {
            depth++;
    
            if (!from.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                to += Path.DirectorySeparatorChar;
            }
            if (!to.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                to += Path.DirectorySeparatorChar;
            }
    
            System.Diagnostics.Debug.WriteLine(string.Format("RecursiveDirectoryCopy( {0}, {1}, {2} )", from, to, merge));
            if (Directory.Exists(to))
            {
                if (!merge)
                {
                    return (int)EventEnum.FileSystemError_DirectoryAlreadyExists;
                }
            }
            else
            {
                Directory.CreateDirectory(to);
            }
    
            string[] directories = Directory.GetDirectories(from);
    
            foreach (string fromDirectory in directories)
            {
                string [] fromDirectoryComponents = fromDirectory.Split(Path.DirectorySeparatorChar);
                string toDirectory = to + fromDirectoryComponents[fromDirectoryComponents.Length - 1];
                RecursiveCopyDirectory(fromDirectory, toDirectory, merge, overwrite, depth);
            }
    
            string[] files = Directory.GetFiles(from);
    
            foreach (string fromFile in files)
            {
                string fileName = Path.GetFileName(fromFile);
                //System.Diagnostics.Debug.WriteLine(string.Format("Name: {0}", to + fileName));    
                string toFile = to + fileName;
    
                File.Copy(fromFile, toFile, overwrite);
            }
    
            return (int)EventEnum.GeneralSuccess;
        }
    
    • hyp
      hyp over 12 years
      1st thought - simple networking issue, but some code could be helpful.
  • JMorgan
    JMorgan over 12 years
    Thanks, I will look into this.
  • Ram
    Ram over 10 years
    @Priyank The MSDN recommendation link does not say anywhere not rely on overwrite and use delete + copy instead. Can you please recheck if the link you provided is not updated or can you provide any reason for the same. ( i am also facing similar error)