我有以下代码,使用
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
}