【发布时间】: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 进程可能会寻找另一个进程打开的文件?)