I have an uploader that I use to split files up and upload them to my sql server. I then download each chunk and create a temporary file. I am trying to write a list of byte arrays(byte[]) into one file to recreate that file. This is because when I try to read the list of byte arrays into one array, I get an OutOfMemory exception. I am just wondering what the best way to do this is. Thanks!
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
int currentRowSelection = fUS_FileDataGridView.CurrentCell.RowIndex;
var totalNumber = fUS_FileDataGridView.Rows[currentRowSelection].Cells[6].Value;
for (int i = 1; i < 149; i++)
{
using (var stream1 = new FileStream(path + @"\" + i + ".zip", FileMode.Open, FileAccess.Read))
{
using (var reader = new BinaryReader(stream1))
{
list_.Add(reader.ReadBytes((int)stream1.Length));
stream1.Close();
stream1.Dispose();
reader.Close();
reader.Dispose();
}
}
}
//array_ = list_.SelectMany(a => a).ToArray();
filePaths_ = @"C:\Users\ATLAS\Desktop\13\fun.zip";
foreach (byte[] bytes in list_)
{
var doc = System.Text.Encoding.Default.GetString(bytes);
string textToAdd1 = bytes.ToString();
try
{
using (FileStream fs = File.Create(filePaths_))
using (StreamWriter writer = new StreamWriter(fs, Encoding.Default, 512))
{
writer.Write(textToAdd1);
writer.Close();
writer.Dispose();
}
}
finally
{
}
}
}
Update: my question is different from the others I have found because I cannot put my list of byte arrays into a single array to write a file. I am currently only getting a 1 KB file out of my code where I should be getting a 100KB file.
Update 2: The code below makes much more sense but now I am getting a "stream was not writable error"
filePaths_ = @"C:\Users\ATLAS\Desktop\13\fun.zip";
using (FileStream fs = File.Create(filePaths_))
for (int i = 0; i < 151; i++)
{
using (var stream1 = new FileStream(path + @"\" + i + ".zip", FileMode.Open, FileAccess.Read))
{
using (var reader = new BinaryReader(stream1))
{
using (StreamWriter writer = new StreamWriter(fs, Encoding.Default, 512))
{
writer.Write(reader);
}
}
}
}