【问题标题】:System.IO.File.Copy() produces System.IO.IOException: The specified network name is no longer availableSystem.IO.File.Copy() 产生 System.IO.IOException:指定的网络名称不再可用
【发布时间】:2011-04-21 14:12:48
【问题描述】:

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

复制的数据大小从 50KB 到 750MB 不等,复制的文件数量不等。在大约 20% 的副本中,我得到了 System.IO.IOException: The specified network name is no longer available。

我的 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;
    }

【问题讨论】:

  • 第一个想法 - 简单的网络问题,但一些代码可能会有所帮助。
  • 更多搜索显示一些人在本地安全策略中禁用双方的数字签名通信,但由于这是在域上,我不知道这是否会被视为“可接受” ' 解决方案。
  • 同一个文件第一次复制失败会不会第二次失败?
  • 仅供参考:您的第一个 if 语句将“\”添加到“to”变量而不是“from”。此外,您还有一个深度变量,您永远不会使用它来防止任何最大递归级别。
  • 此服务是否会在复制后删除文件? (或其他进程是否这样做?)此服务是否在多个主机上运行? (是否有竞争的 IO 进程可能会寻找另一个进程打开的文件?)

标签: c# .net-3.5 .net


【解决方案1】:

File.Copy() 打开下划线流。您可能在 File.Copy() 正在进行时失去了连接。所以,它不能刷新和关闭流。

从中恢复的一种可能性是使用 FileStream 类和 拨打Win32 API CloseHandle when such exception occurs,这样做会释放 操作系统文件句柄,以便您可以重新打开文件。

[ 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 overwriteTry 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.

【讨论】:

  • 谢谢,我会调查的。
  • @Priyank MSDN 推荐链接没有说任何地方不依赖覆盖,而是使用删除 + 复制。您能否重新检查您提供的链接是否未更新,或者您能否提供任何原因。 (我也面临类似的错误)
【解决方案2】:

该错误似乎表明网络连接在中途丢失,可能根本与代码无关。如果同一文件夹复制有时成功而有时失败,那么这将证明这不是应归咎的代码,而必须是资源访问问题。

【讨论】:

  • 我们正在对此进行调查,但到目前为止,连接似乎并没有丢失。
【解决方案3】:

事实证明,使用该软件的客户同时针对同一数据集运行该软件的两个实例。一旦冗余实例停止,它就解决了错误。感谢所有回答的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-13
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 2023-03-23
    • 1970-01-01
    • 2011-12-24
    相关资源
    最近更新 更多