如何在没有CopyFile或者CopyFileEx的Windows上复制大文件?
时间:2020-03-06 14:21:06 来源:igfitidea点击:
Windows Server 2003上有一个限制,它可以防止我们按拥有的RAM数量成比例地复制非常大的文件。限制在Copyfile和CopyFileEx函数中,xcopy,Explorer,Robocopy和.NET FileInfo类使用它们。
这是我们得到的错误:
Cannot copy [filename]: Insufficient system resources exist to complete the requested service.
是有关此主题的知识库文章,但它与NT4和2000有关。
还建议从Exchange安装中使用ESEUTIL,但我没有运气让它正常工作。
有谁知道快速,简便的方法来解决这个问题?我说的是在具有2Gb RAM的计算机上的> 50Gb。我打算启动Visual Studio并为我写一些东西来做,但是如果已经有了一些稳定且经过测试的东西,那将是一件很不错的事情。
[编辑]我提供了有效的Ccode来接受已接受的答案。
解决方案
最好的选择是只打开原始文件进行读取,打开目标文件进行写入,然后逐块循环复制。用伪代码:
f1 = open(filename1); f2 = open(filename2, "w"); while( !f1.eof() ) { buffer = f1.read(buffersize); err = f2.write(buffer, buffersize); if err != NO_ERROR_CODE break; } f1.close(); f2.close();
[由Asker编辑]好的,这就是它在C语言中的外观(虽然很慢,但似乎可以正常工作,并且可以取得进展):
using System; using System.Collections.Generic; using System.IO; using System.Text; namespace LoopCopy { class Program { static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine( "Usage: LoopCopy.exe SourceFile DestFile"); return; } string srcName = args[0]; string destName = args[1]; FileInfo sourceFile = new FileInfo(srcName); if (!sourceFile.Exists) { Console.WriteLine("Source file {0} does not exist", srcName); return; } long fileLen = sourceFile.Length; FileInfo destFile = new FileInfo(destName); if (destFile.Exists) { Console.WriteLine("Destination file {0} already exists", destName); return; } int buflen = 1024; byte[] buf = new byte[buflen]; long totalBytesRead = 0; double pctDone = 0; string msg = ""; int numReads = 0; Console.Write("Progress: "); using (FileStream sourceStream = new FileStream(srcName, FileMode.Open)) { using (FileStream destStream = new FileStream(destName, FileMode.CreateNew)) { while (true) { numReads++; int bytesRead = sourceStream.Read(buf, 0, buflen); if (bytesRead == 0) break; destStream.Write(buf, 0, bytesRead); totalBytesRead += bytesRead; if (numReads % 10 == 0) { for (int i = 0; i < msg.Length; i++) { Console.Write("\b \b"); } pctDone = (double) ((double)totalBytesRead / (double)fileLen); msg = string.Format("{0}%", (int)(pctDone * 100)); Console.Write(msg); } if (bytesRead < buflen) break; } } } for (int i = 0; i < msg.Length; i++) { Console.Write("\b \b"); } Console.WriteLine("100%"); Console.WriteLine("Done"); } } }
如果要编写代码,可以优化的一种方法是分块发送文件(例如使用MTOM)。我使用这种方法将大型文件从数据中心发送到我们的办公室进行打印。
另外,请检查此处提到的TeraCopy实用程序。