combine large files together

I have several files on my hard drive that need to be put back together as one file and written back to the hard drive. the files total over 2GB so it is producing the following error while i work with them: “OutOfMemoryException: Out of memory.” i think my code is simply taking too much ram.

the files are named “part01.tdd”, “part02.tdd” ,“part03.tdd” and so on. They simply need to be read into byte arrays and stacked together end to end to make a file!

Anyone have any other ideas?

 i=0;bt=new byte[0];
while(i>-1){
   if(i<10){txt="0"+i;}else{txt=""+i;}
   txt=dir+"/"+"part"+txt+".tdd";
   if(File.Exists(txt)){
         bt=bt+File.ReadAllBytes(txt);// <--error is here
         i++;}
         else{i=-1;}
 }
 print("saving"+bt.Length);
 File.WriteAllBytes(dir+"/"+dstring+"/"+dstring+".mp4",bt);

You have to use a loop and copy only a portion at a time. Also you can’t simply “add” byte arrays together. What you should do is something like that:

// this array should hold all input filenames.
string[] inFileNames;

byte[] buffer = new byte[8192];
using (var outStream = System.IO.File.OpenWrite(YourTargetFileName))
{
    for (int i = 0; i < inFileNames.Length; i++)
    {
        using (var inStream = System.IO.File.OpenRead(inFileNames*))*

{
int countRead;
while(true)
{
countRead = inStream.Read(buffer, 0, buffer.Length);
if (countRead>0)
outStream.Write(buffer, 0, countRead);
if (countRead < buffer.Length)
break;
}
}
}
}