您如何从MemoryStream获取字符串?


532

如果给我一个MemoryStream我知道已填充的String,我该如何String退出?


1
永远不确定是否始终需要reader.close。过去我遇到过很多问题,因此通常我总是为了安全起见。
Crusty

Answers:


468

此示例说明如何向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

3
当函数超出范围时,是否将不处理StreamWriter?
Tim Keating 2010年

14
当变量超出范围时,不调用Dispose。当GC处理完后,将调用Finalize,但必须在变量超出范围之前调用Dispose。我没有在上面调用它,因为我知道StreamWriter和StreamReader的实现不需要调用Dispose,它只是将调用传递给基础流。但是,可以为调用IDpose的任何实现IDisposable的实现提供合理的论据,因为您不能保证将来的发行版不会要求将其处置。
Brian

12
@MichaelEakins当问题标记为VB.Net时,为什么答案甚至应该是C#?
罗兰·肖

1
我很高兴得知“帮助程序”将dispose调用传递给其基础流,但是这似乎是一个错误的设计决策。
Gerard ONeill

2
:这个决定是缓解以后msdn.microsoft.com/en-us/library/...
马克Sowul

310

您也可以使用

Encoding.ASCII.GetString(ms.ToArray());

认为这样做效率不高,但我不能对此宣誓。它还允许您选择其他编码,而使用StreamReader则必须将其指定为参数。


6
编码在System.Text命名空间中
northben 2014年

2
我一直在寻找与之等效的PowerShell,因此不得不使用它。([System.Text.Encoding] :: ASCII).GetString(ms.ToArray())
Lewis

此解决方案很有用,因为可以在MemoryStream关闭后使用它。
雅各布·霍布利克

2
FWIW,我发现这不适用于非常大的字符串,我得到OutOfMemoryExceptions了。使用StreamReader代替解决了问题。
Grant H.

由于这可能是一个陷阱:它不知道字节顺序标记,并且可能00在字符串的开头包含十六进制。00 3C 3F-> .<?在Hex Editor中,但在VS或Notepad ++中:<?。因此,即使您通过肉眼比较字符串也看不到差异,只有比较工具或十六进制编辑器才能显示差异。如果仍然使用它,请考虑使用String.TrimStart。请参阅:docs.microsoft.com/zh-cn/dotnet/api/…–
Skalli,

99

使用StreamReader将MemoryStream转换为字符串。

<Extension()> _
Public Function ReadAll(ByVal memStream As MemoryStream) As String
    ' Reset the stream otherwise you will just get an empty string.
    ' Remember the position so we can restore it later.
    Dim pos = memStream.Position
    memStream.Position = 0

    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

3
将Position设置为0会限制该方法的重用能力-最好让调用方对其进行管理。如果流在字符串之前包含数据(调用方知道如何处理),该怎么办?
亚历克斯·莱曼

1
using语句将确保您的StreamReader被处置,但是文档说StreamReader在处置后将关闭基础流。因此,您的方法关闭了它所传递的MemoryStream,即使我怀疑MemoryStream.Dispose做了很多,从概念上讲,这对于调用者来说也是很酷的事情。
Trillian

你是对的。在流帮助器类上使用Dispose方法通常是一个坏主意,特别是如果将流作为参数传递给方法时,尤其如此。我将更新此答案。我也在下面有一个更完整的答案。
布赖恩

如果对这些类进行反编译,则会发现dispose方法
仅对


26
byte[] array = Encoding.ASCII.GetBytes("MyTest1 - MyTest2");
MemoryStream streamItem = new MemoryStream(array);

// convert to string
StreamReader reader = new StreamReader(streamItem);
string text = reader.ReadToEnd();

22

以前的解决方案在涉及编码的情况下不起作用。这是一种“现实生活”,例如如何正确执行此操作的示例...

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()));
}

15

在这种情况下,如果您真的想以一种简单的方式使用ReadToEnd方法MemoryStream,则可以使用此扩展方法来实现:

public static class SetExtensions
{
    public static string ReadToEnd(this MemoryStream BASE)
    {
        BASE.Position = 0;
        StreamReader R = new StreamReader(BASE);
        return R.ReadToEnd();
    }
}

您可以通过以下方式使用此方法:

using (MemoryStream m = new MemoryStream())
{
    //for example i want to serialize an object into MemoryStream
    //I want to use XmlSeralizer
    XmlSerializer xs = new XmlSerializer(_yourVariable.GetType());
    xs.Serialize(m, _yourVariable);

    //the easy way to use ReadToEnd method in MemoryStream
    MessageBox.Show(m.ReadToEnd());
}

11

此示例显示如何从MemoryStream中读取字符串,在其中使用了序列化(使用DataContractJsonSerializer),将字符串从某些服务器传递到客户端,然后如何从作为参数传递的字符串中恢复MemoryStream,然后,反序列化MemoryStream。

我使用了不同帖子的一部分来执行此示例。

希望这会有所帮助。

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading;

namespace JsonSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var phones = new List<Phone>
            {
                new Phone { Type = PhoneTypes.Home, Number = "28736127" },
                new Phone { Type = PhoneTypes.Movil, Number = "842736487" }
            };
            var p = new Person { Id = 1, Name = "Person 1", BirthDate = DateTime.Now, Phones = phones };

            Console.WriteLine("New object 'Person' in the server side:");
            Console.WriteLine(string.Format("Id: {0}, Name: {1}, Birthday: {2}.", p.Id, p.Name, p.BirthDate.ToShortDateString()));
            Console.WriteLine(string.Format("Phone: {0} {1}", p.Phones[0].Type.ToString(), p.Phones[0].Number));
            Console.WriteLine(string.Format("Phone: {0} {1}", p.Phones[1].Type.ToString(), p.Phones[1].Number));

            Console.Write(Environment.NewLine);
            Thread.Sleep(2000);

            var stream1 = new MemoryStream();
            var ser = new DataContractJsonSerializer(typeof(Person));

            ser.WriteObject(stream1, p);

            stream1.Position = 0;
            StreamReader sr = new StreamReader(stream1);
            Console.Write("JSON form of Person object: ");
            Console.WriteLine(sr.ReadToEnd());

            Console.Write(Environment.NewLine);
            Thread.Sleep(2000);

            var f = GetStringFromMemoryStream(stream1);

            Console.Write(Environment.NewLine);
            Thread.Sleep(2000);

            Console.WriteLine("Passing string parameter from server to client...");

            Console.Write(Environment.NewLine);
            Thread.Sleep(2000);

            var g = GetMemoryStreamFromString(f);
            g.Position = 0;
            var ser2 = new DataContractJsonSerializer(typeof(Person));
            var p2 = (Person)ser2.ReadObject(g);

            Console.Write(Environment.NewLine);
            Thread.Sleep(2000);

            Console.WriteLine("New object 'Person' arrived to the client:");
            Console.WriteLine(string.Format("Id: {0}, Name: {1}, Birthday: {2}.", p2.Id, p2.Name, p2.BirthDate.ToShortDateString()));
            Console.WriteLine(string.Format("Phone: {0} {1}", p2.Phones[0].Type.ToString(), p2.Phones[0].Number));
            Console.WriteLine(string.Format("Phone: {0} {1}", p2.Phones[1].Type.ToString(), p2.Phones[1].Number));

            Console.Read();
        }

        private static MemoryStream GetMemoryStreamFromString(string s)
        {
            var stream = new MemoryStream();
            var sw = new StreamWriter(stream);
            sw.Write(s);
            sw.Flush();
            stream.Position = 0;
            return stream;
        }

        private static string GetStringFromMemoryStream(MemoryStream ms)
        {
            ms.Position = 0;
            using (StreamReader sr = new StreamReader(ms))
            {
                return sr.ReadToEnd();
            }
        }
    }

    [DataContract]
    internal class Person
    {
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public DateTime BirthDate { get; set; }
        [DataMember]
        public List<Phone> Phones { get; set; }
    }

    [DataContract]
    internal class Phone
    {
        [DataMember]
        public PhoneTypes Type { get; set; }
        [DataMember]
        public string Number { get; set; }
    }

    internal enum PhoneTypes
    {
        Home = 1,
        Movil = 2
    }
}

5

稍作修改的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

3
它确实增加了什么新的布赖恩的答案
路易斯·菲利普

5

为什么不对MemoryStream类型做一个很好的扩展方法呢?

public static class MemoryStreamExtensions
{

    static object streamLock = new object();

    public static void WriteLine(this MemoryStream stream, string text, bool flush)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(text + Environment.NewLine);
        lock (streamLock)
        {
            stream.Write(bytes, 0, bytes.Length);
            if (flush)
            {
                stream.Flush();
            }
        }
    }

    public static void WriteLine(this MemoryStream stream, string formatString, bool flush, params string[] strings)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(String.Format(formatString, strings) + Environment.NewLine);
        lock (streamLock)
        {
            stream.Write(bytes, 0, bytes.Length);
            if (flush)
            {
                stream.Flush();
            }
        }
    }

    public static void WriteToConsole(this MemoryStream stream)
    {
        lock (streamLock)
        {
            long temporary = stream.Position;
            stream.Position = 0;
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, false, 0x1000, true))
            {
                string text = reader.ReadToEnd();
                if (!String.IsNullOrEmpty(text))
                {
                    Console.WriteLine(text);
                }
            }
            stream.Position = temporary;
        }
    }
}

当然,将这些方法与标准方法结合使用时要小心。:) ...如果需要,您需要使用该方便的streamLock来实现并发。


0

我需要与需要在其上编写流的类进行集成:

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);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.