System.IO.File.Copy()产生System.IO.IOException:指定的网络名称不再可用

本文关键字:System IO 网络 不再 Copy File IOException 产生 | 更新日期: 2023-09-27 17:52:48

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

拷贝的数据大小范围为50KB ~ 750MB,拷贝的文件个数不同。在大约20%的副本中,我得到System.IO.IOException:指定的网络名称不再可用。

我的google-fu未能在文件复制期间找到可能导致此问题的答案。有人见过/解决过这个问题吗?

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

System.IO.File.Copy()产生System.IO.IOException:指定的网络名称不再可用

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建议不要依赖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.

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

结果是,使用该软件的客户同时针对同一数据集运行了该软件的两个实例。一旦停止了冗余实例,它就解决了错误。