Hi,
MemoryStream.ToArray Method
Writes the stream contents to a byte array, regardless of the Position property.
Below is a test sample base on MemoryStream . Create a stream and read 20 bytes. Then copy the stream.
// Copies the entire stream into a byte array
byte[] buffer = memStream.ToArray();
using (MemoryStream tempStream = new MemoryStream(buffer))
Sample:
using System;
using System.IO;
using System.Text;
class MemStream
{
static void Main()
{
int count;
byte[] byteArray;
char[] charArray;
UnicodeEncoding uniEncoding = new UnicodeEncoding();
// Create the data to write to the stream.
byte[] firstString = uniEncoding.GetBytes(
"Invalid file path characters are: ");
byte[] secondString = uniEncoding.GetBytes(
Path.GetInvalidPathChars());
using (MemoryStream memStream = new MemoryStream(100))
{
// Write the first string to the stream.
memStream.Write(firstString, 0, firstString.Length);
// Write the second string to the stream, byte by byte.
count = 0;
while (count < secondString.Length)
{
memStream.WriteByte(secondString[count++]);
}
// Write the stream properties to the console.
Console.WriteLine(
"Capacity = {0}, Length = {1}, Position = {2}\n",
memStream.Capacity.ToString(),
memStream.Length.ToString(),
memStream.Position.ToString());
// Set the position to the beginning of the stream.
memStream.Seek(0, SeekOrigin.Begin);
// Read the first 20 bytes from the stream.
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);
/************************************************************************************************************/
// Copies the entire stream into a byte array
byte[] buffer = memStream.ToArray();
using (MemoryStream tempStream = new MemoryStream(buffer))
{
Console.WriteLine("memstream pos is :"+memStream.Position.ToString());
byte[] byteArray2;
char[] charArray2;
tempStream.Seek(0, SeekOrigin.Begin);
byteArray2 = new byte[tempStream.Length];
var count2 = tempStream.Read(byteArray2, 0, 20);
charArray2 = new char[uniEncoding.GetCharCount(
byteArray2, 0, count)];
uniEncoding.GetDecoder().GetChars(
byteArray2, 0, count, charArray2, 0);
Console.WriteLine(charArray2);
Console.WriteLine("temp stream end, below is memstream\n");
/***********************************************************************************************/
// Read the remaining bytes, byte by byte.
while (count < memStream.Length)
{
byteArray[count++] = (byte)memStream.ReadByte();
}
// Decode the byte array into a char array
// and write it to the console.
charArray = new char[uniEncoding.GetCharCount(
byteArray, 0, count)];
uniEncoding.GetDecoder().GetChars(
byteArray, 0, count, charArray, 0);
Console.WriteLine(charArray);
}
}
}
}
Best regards,
Minxin Yu
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.