Serializing a memorystream object to string

六眼飞鱼酱① 提交于 2019-12-01 16:06:14
Stecya
using(MemoryStream stream = new MemoryStream()) {
   stream.Position = 0;
   var sr = new StreamReader(stream);
   string myStr = sr.ReadToEnd();
}

You cant use GetBuffer when you use MemoryStream(byte[]) constructor.

MSDN quote:

This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException.

You must use this constructor and set publiclyVisible = true in order to use GetBuffer

In VB.net i used this

Dim TempText = System.Text.Encoding.UTF8.GetString(TempMemoryStream.ToArray())

in C# may apply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!