首页 > 代码库 > StreamWriter寫入MemoryStream 讀不到數據

StreamWriter寫入MemoryStream 讀不到數據

1 MemoryStream ms = new MemoryStream();
2 StreamWriter sw = new StreamWriter(ms, Encoding.Default);
sw.AutoFlush = true;
3 sw.WriterLine("....."); 4 . 5 . 6 . 7 byte[] buff = new byte[1024]; 8 int count = ms.Read(buff, 0, 1024);

 

count 始終為0,buff里也讀不到數據。
在 stackoverflow.com 查到得知讀取流前,
要把流的當前位置設為0, ms.Position = 0;

問題解決。

StreamWriter寫入MemoryStream 讀不到數據