title: System.IO.File.Copy() produces System.IO.IOException: The specified network name is no longer available
body: <p>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.</p>
  
  <p>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.  </p>
  
  <p>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?</p>
  
  <p>Here is the recursive method that does the copy.  The exception occurs on line File.Copy(fromFile, toFile, overwrite);</p>
  
  <pre><code>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;
      }
  </code></pre>