fenri's diary

基本的には勉強し始めたC#のメモ。後は140字で収まらない駄文。

StreamWriter で正常にWriteされない問題

MemoryStream へ書いた値が途中で切れてしまう。
原因はStreamWriterのFlushがない


問題のソース

this.srm = new System.IO.MemoryStream();

System.IO.StreamWriter writer = 
 new System.IO.StreamWriter(this.srm, System.Text.Encoding.Default);
writer.Write(txt);
writer = null;

修正ソース

this.srm = new System.IO.MemoryStream();

System.IO.StreamWriter writer = 
 new System.IO.StreamWriter(this.srm, System.Text.Encoding.Default);
writer.Write(txt);
writer.Flush();
writer = null;


StreamWriter.Flush メソッド
現在のライターで使用したすべてのバッファーをクリアし、バッファー内のすべてのデータを基になるストリームに書き込みます。