日期:2014-05-17 浏览次数:20900 次
//写入并读取.
void WriteAndRead(object sender, EventArgs e) {
byte[] bytesFromEncodingInput = Encoding.Default.GetBytes(txtInput.Text); //选择默认编码.
MemoryStream mStream = new MemoryStream(20);
mStream.Write(bytesFromEncodingInput, 0, bytesFromEncodingInput.Length); //写入内存流.
richTxtProperties.Text = string.Format("分配给内存流的字节数为 {0},流长度为 {1},当前流位置为 {2}", mStream.Capacity, mStream.Length, mStream.Position);
mStream.Seek(0, SeekOrigin.Begin); //将流的位置设为最开始.
int readPosition = 0; //流的位置.
while(readPosition < mStream.Length - 1) {
byte[] bytesFromMStreamRead = new byte[5]; //流每次读取后存放的数组.
mStream.Read(bytesFromMStreamRead, readPosition, bytesFromMStreamRead.Length);
char[] cResult = new char[Encoding.Default.GetCharCount(bytesFromMStreamRead)]; //存放解析后的字节数组.
Encoding.Default.GetChars(bytesFromMStreamRead, readPosition, bytesFromMStreamRead.Length, cResult, 0);
foreach(var r in cResult) {
richTxtContent.Text += r.ToString();
}
mStream.Flush(); //情况缓冲区.
}
}
/*
为了能分段给用户返回内存读取数据,而不是一致性读取后返回给用户,我这样做.
我调试的时候,发现我的"richTxtContent"的Text"是我输入的哈....
但是运行的时候,无法回到页面.