通过本地 LAN 共享复制大约 50 GB 的数据时,由于连接问题,复制在大约 10 GB 时失败。
我已将复制的 10GB 数据目录重命名为 localRepository,然后编写了一个 C# 程序将文件从远程服务器复制到目标,前提是在本地存储库中找不到它。如果找到将文件从本地存储库移动到目标文件夹。
尽管代码运行良好并且很好地完成了任务。我想知道,我是否编写了最有效的代码?你能找到任何改进吗?
string destinationFolder = @"C:\DataFolder";
string remoteRepository = @"\\RemoteComputer\DataFolder";
string localRepository = @"\\LocalComputer\LocalRepository";
protected void Page_Load(object sender, EventArgs e)
{
foreach (string remoteSrcFile in Directory.EnumerateFiles(remoteRepository, "*.*", SearchOption.AllDirectories))
{
bool foundInLocalRepo = false; ;
foreach (var localSrcFile in Directory.EnumerateFiles(localRepository, "*.*", SearchOption.AllDirectories))
{
if (Path.GetFileName(remoteSrcFile).Equals(Path.GetFileName(localSrcFile)))
{
FileInfo localFile = new FileInfo(localSrcFile);
FileInfo remoteFile = new FileInfo(remoteSrcFile);
//copy this file from local repository
if (localFile.Length == remoteFile.Length)
{
try
{
File.Move(localSrcFile, PrepareDestinationPath(remoteSrcFile));
Debug.WriteLine(remoteSrcFile + " moved from local repo");
}
catch (Exception ex)
{
Debug.WriteLine(remoteSrcFile + " did not move");
}
foundInLocalRepo = true;
break;
}
}
}
if (!foundInLocalRepo)
{
//copy this file from remote repository
try
{
File.Copy(remoteSrcFile, PrepareDestinationPath(remoteSrcFile), false);
Debug.WriteLine(remoteSrcFile + " copied from remote repo");
}
catch (Exception ex)
{
Debug.WriteLine(remoteSrcFile + " did not copy");
}
}
}
}
private string PrepareDestinationPath(string remoteSrcFile)
{
string relativePath = remoteSrcFile.Split(new string[] { "DataFolder" }, StringSplitOptions.None)[1];
string copyPath = Path.GetFullPath(destinationFolder + relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(copyPath));
return copyPath;
}
编辑:
根据 Thomas 给出的回答,我正在尝试压缩文件。传统上,作为最终用户,我们使用压缩文件然后复制。作为程序员,我们可以并行压缩和复制文件吗?我的意思是已经压缩的部分通过电线发送?