可能重复:
从流中创建字节数组
我想在内存中创建文本文件并将其写入byte[]
,应该怎么做?
public byte[] GetBytes()
{
MemoryStream fs = new MemoryStream();
TextWriter tx = new StreamWriter(fs);
tx.WriteLine("1111");
tx.WriteLine("2222");
tx.WriteLine("3333");
tx.Flush();
fs.Flush();
byte[] bytes = new byte[fs.Length];
fs.Read(bytes,0,fs.Length);
return bytes;
}
但由于数据长度的原因,它无法正常工作。