c# - System.IO.File.Copy() 产生 System.IO.IOException : The specified network name is no longer available

标签 c# .net-3.5 .net

我有一个 Windows 服务 (C# .Net 3.5),它从网络共享获取数据并将其复制到服务的主机。

复制的数据大小从50KB到750MB不等,复制的文件数量也不同。在大约 20% 的副本中,我收到 System.IO.IOException: 指定的网络名称不再可用。

我的 google-fu 未能找到有关在 File.Copy 期间可能导致此问题的原因的答案。以前有人见过/解决过这个问题吗?

这是执行复制的递归方法。异常发生在 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;
    }

最佳答案

File.Copy() 打开下划线流。当 File.Copy() 正在进行时,您可能失去了连接。因此,它无法刷新并关闭流。

解决此问题的一种可能方法是使用 FileStream 类并 发生此类异常时调用Win32 API CloseHandle,这样做会释放 操作系统文件句柄,以便您可以重新打开该文件。

[ 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);
}

此外,MSDN recommends not to rely on overwrite尝试删除现有文件并在复制时创建新文件。

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.

关于c# - System.IO.File.Copy() 产生 System.IO.IOException : The specified network name is no longer available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5745293/

相关文章:

c# - 为什么 JITted Python 实现仍然很慢?

c# - 关闭 FileStream 会关闭 StreamReader 吗?

c# - 使用 CodeDom 完成终结器?

c# - 垂直翻转字节数组中位图的算法

c# - C# 是否具有与#def 常量等效的#include?

c# - 如何在Windows Phone中用一个单词写出不同颜色的文本?

c# - EFv1 一对多映射与 POCO 的关系

c# - 在给定时间后等待并终止线程而不阻塞.NET 3.5

c# - 将十六进制字符串转换为日期时间的困难

c# - 将 URL 限制为特定数据库用户名 ASP.NET