资 源 简 介
C# 写入并读取内存流,演示一些基本的内存流操作方法,编写以下代码可实现这些功能:
byte[] BContent = Encoding.Default.GetBytes(textBox1.Text);
MemoryStream MStream = new MemoryStream(100);
MStream.Write(BContent, 0, BContent.Length);
richTextBox1.Text = "分配给该流的字节数:" + MStream.Capacity.ToString() + "
流长度:"
+ MStream.Length.ToString() + "
流的当前位置:" + MStream.Position.ToString();
MStream.Seek(0, SeekOrigin.Begin);
byte[] byteArray = new byte[MStream.Length];
int count = MStream.Read(byteArray,0,(int)MStream.Length-1);
while (count < MStream.Length)
{
byteArray[count++] = Convert.ToByte(MStream.ReadByte());
}
char[] charArray = new char[Encoding.Default.GetCharCount(byteArray, 0, count)];
Encoding.Default.GetChars(byteArray, 0, count, charArray, 0);
for (int i = 0; i < charArray.Length; i++)
{
richTextBox2.Text += charArray[i].ToString();
}