![](/img/trans.png)
[英]How do I get CopyFileEx to report back so I can cancel a file copy operation?
[英]How can I copy a large file on Windows without CopyFile or CopyFileEx?
Windows Server 2003存在一個限制,它會阻止您復制超大文件,與您擁有的RAM量成比例。 限制在CopyFile和CopyFileEx函數中,xcopy,Explorer,Robocopy和.NET FileInfo類使用這些函數。
這是你得到的錯誤:
無法復制[filename]:系統資源不足,無法完成所請求的服務。
這是關於該主題的知識庫文章 ,但它涉及NT4和2000。
還有一個建議是從Exchange安裝中使用ESEUTIL ,但我沒有運氣這么做。
有人知道一個快速,簡單的方法來處理這個問題嗎? 我在談論帶有2Gb RAM的機器上> 50Gb。 我計划啟動Visual Studio並為我編寫一些東西,但是有一些已經存在的東西,穩定且經過充分測試會很好。
[編輯]我提供了工作C#代碼以配合接受的答案。
最好的選擇是打開原始文件進行讀取,寫入目標文件,然后逐塊循環復制。 在偽代碼中:
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");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.