Skip to content

Instantly share code, notes, and snippets.

@unter
Created March 5, 2019 16:59
Show Gist options
  • Select an option

  • Save unter/3aa0b0c35c8fe3fc29ae54725c168f20 to your computer and use it in GitHub Desktop.

Select an option

Save unter/3aa0b0c35c8fe3fc29ae54725c168f20 to your computer and use it in GitHub Desktop.
private static void MemoryStreamTest()
{
string localFilePath = @"c:\tmp";
try
{
using (MemoryStream memoryStream = new MemoryStream())
{
for (int i = 0; i< 256;i++)
{
string tempFilePath = Path.Combine(localFilePath, $"file{i}");
CreateRandomTempFile(tempFilePath, 8);
using (FileStream fs = new FileStream(tempFilePath, FileMode.Open))
{
fs.CopyTo(memoryStream);
}
File.Delete(tempFilePath);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception in MemoryStreamTest: {ex.Message}");
Console.ReadLine();
}
}
private static void CreateRandomTempFile(string fileName, long lengthInMb)
{
// https://stackoverflow.com/questions/4432178/creating-a-random-file-in-c-sharp
byte[] data = new byte[lengthInMb * 1024 * 1024];
Random rng = new Random();
rng.NextBytes(data);
File.WriteAllBytes(fileName, data);
}
@StefH
Copy link
Copy Markdown

StefH commented Apr 21, 2020

Thanks for you blogpost!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment