Hi @דני שטרית , Welcome to Microsoft Q&A,
Updated:
The problem solved. The code is good.the problematic USB was not of NTFS type. Ntfs allow to copy more than 4 Giga.
Regarding the copying of large files, since large files cannot be completely loaded into memory, you cannot use the File pointer to operate it.
In c#, you can use FileStream to
accomplish this requirement, because FileStream
allows reading and writing files in chunks, that is, splitting large files into small chunks and writing them into new files in segments.
You can refer to the following code. After testing, this code takes about 28 seconds to copy a 6G file.
public static class FileCopyHelper
{
public static void CopyBigFile(string sSource, string sTarget)
{
using (FileStream fsRead = new FileStream(sSource, FileMode.Open, FileAccess.Read))
{
using (FileStream fsWrite = new FileStream(sTarget, FileMode.Create, FileAccess.Write))
{
//define buffer If the buffer is too large, the memory usage will be too high and the computer will freeze.
byte[] bteData = new byte[12 * 1024 * 1024];
int r = fsRead.Read(bteData, 0, bteData.Length);
while (r > 0)
{
fsWrite.Write(bteData, 0, r);
double d = 100 * (fsWrite.Position / (double)fsRead.Length);
Console.WriteLine("{0}%", d);
r = fsRead.Read(bteData, 0, bteData.Length);
}
Console.WriteLine("Copying the large file successfully");
}
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.