如果我给了一个MemoryStream,我知道已经填充了一个字符串,我如何得到一个字符串回来?
当前回答
这个示例展示了如何将字符串读写到MemoryStream。
Imports System.IO
Module Module1
Sub Main()
' We don't need to dispose any of the MemoryStream
' because it is a managed object. However, just for
' good practice, we'll close the MemoryStream.
Using ms As New MemoryStream
Dim sw As New StreamWriter(ms)
sw.WriteLine("Hello World")
' The string is currently stored in the
' StreamWriters buffer. Flushing the stream will
' force the string into the MemoryStream.
sw.Flush()
' If we dispose the StreamWriter now, it will close
' the BaseStream (which is our MemoryStream) which
' will prevent us from reading from our MemoryStream
'sw.Dispose()
' The StreamReader will read from the current
' position of the MemoryStream which is currently
' set at the end of the string we just wrote to it.
' We need to set the position to 0 in order to read
' from the beginning.
ms.Position = 0
Dim sr As New StreamReader(ms)
Dim myStr = sr.ReadToEnd()
Console.WriteLine(myStr)
' We can dispose our StreamWriter and StreamReader
' now, though this isn't necessary (they don't hold
' any resources open on their own).
sw.Dispose()
sr.Dispose()
End Using
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
End Sub
End Module
其他回答
以前的解决方案在涉及编码的情况下不起作用。这是一个“现实生活”的例子,如何正确地做到这一点…
using(var stream = new System.IO.MemoryStream())
{
var serializer = new DataContractJsonSerializer(typeof(IEnumerable<ExportData>), new[]{typeof(ExportData)}, Int32.MaxValue, true, null, false);
serializer.WriteObject(stream, model);
var jsonString = Encoding.Default.GetString((stream.ToArray()));
}
这个示例展示了如何将字符串读写到MemoryStream。
Imports System.IO
Module Module1
Sub Main()
' We don't need to dispose any of the MemoryStream
' because it is a managed object. However, just for
' good practice, we'll close the MemoryStream.
Using ms As New MemoryStream
Dim sw As New StreamWriter(ms)
sw.WriteLine("Hello World")
' The string is currently stored in the
' StreamWriters buffer. Flushing the stream will
' force the string into the MemoryStream.
sw.Flush()
' If we dispose the StreamWriter now, it will close
' the BaseStream (which is our MemoryStream) which
' will prevent us from reading from our MemoryStream
'sw.Dispose()
' The StreamReader will read from the current
' position of the MemoryStream which is currently
' set at the end of the string we just wrote to it.
' We need to set the position to 0 in order to read
' from the beginning.
ms.Position = 0
Dim sr As New StreamReader(ms)
Dim myStr = sr.ReadToEnd()
Console.WriteLine(myStr)
' We can dispose our StreamWriter and StreamReader
' now, though this isn't necessary (they don't hold
' any resources open on their own).
sw.Dispose()
sr.Dispose()
End Using
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
End Sub
End Module
只使用方法Convert。ToBase64String
Convert.ToBase64String(inputStream.ToArray());
稍微修改了Brian的答案,允许可选的读启动管理,这似乎是最简单的方法。可能不是最有效的,但容易理解和使用。
Public Function ReadAll(ByVal memStream As MemoryStream, Optional ByVal startPos As Integer = 0) As String
' reset the stream or we'll get an empty string returned
' remember the position so we can restore it later
Dim Pos = memStream.Position
memStream.Position = startPos
Dim reader As New StreamReader(memStream)
Dim str = reader.ReadToEnd()
' reset the position so that subsequent writes are correct
memStream.Position = Pos
Return str
End Function
我需要集成一个类,需要一个流写在它:
XmlSchema schema;
// ... Use "schema" ...
var ret = "";
using (var ms = new MemoryStream())
{
schema.Write(ms);
ret = Encoding.ASCII.GetString(ms.ToArray());
}
//here you can use "ret"
// 6 Lines of code
我创建了一个简单的类,可以帮助减少多次使用的代码行:
public static class MemoryStreamStringWrapper
{
public static string Write(Action<MemoryStream> action)
{
var ret = "";
using (var ms = new MemoryStream())
{
action(ms);
ret = Encoding.ASCII.GetString(ms.ToArray());
}
return ret;
}
}
然后可以用一行代码替换示例
var ret = MemoryStreamStringWrapper.Write(schema.Write);
推荐文章
- 如何分割逗号分隔的字符串?
- Java字符串—查看字符串是否只包含数字而不包含字母
- 用javascript检查输入字符串中是否包含数字
- 按类型查找WPF窗口中的所有控件
- 数组与列表的性能
- Java:检查enum是否包含给定的字符串?
- 从Description属性中获取Enum
- 用PHP删除字符串的前4个字符
- 如何从字符串的开始或结束删除所有空白?
- 为什么使用try {} finally{}和一个空的try块?
- 字符串到JS中的对象
- 为什么Python的原始字符串不能以一个反斜杠结尾?
- 如何在内存中获取对象大小?
- 每个优秀的。net开发人员都应该能够回答的问题?
- 我如何读整个文件到性病::字符串在c++ ?