我有以下代码StreamWriter
用于写入MemoryStream
. 但是,当我尝试读回流时,我得到了截断的数据:
using(var outStream = new MemoryStream())
using (var outWriter = new StreamWriter(outStream))
{
// my operation that's writing data to the stream
var outReader = new StreamReader(outStream);
outStream.Flush();
outStream.Position = 0;
return outReader.ReadToEnd();
}
这会返回大部分数据,但会在接近尾声时截断。但是,我知道数据正在进入流中,因为如果我尝试写入文件而不是写入文件,MemoryStream
我会得到全部内容。例如,此代码将整个内容写入文件:
using (var outWriter = new StreamWriter(@"C:\temp\test.out"))
{
// my operation that's writing data to the stream
}