如何从 MemoryStream 中获取字符串?
如果给我一个 MemoryStream
,我知道它已填充了 String
,那么如何取回 String
?
If I am given a MemoryStream
that I know has been populated with a String
, how do I get a String
back out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
// 创建一个 StreamReader 对象以从 MemoryStream 对象中读取数据。
StreamReader 阅读器 = new StreamReader(memoryStream);
// 将 MemoryStream 对象读取为字符串。
MemoryStreamValue = reader.ReadToEnd();
// 关闭 StreamReader 对象。
读者.关闭();
// Create a StreamReader object to read from MemoryStream object.
StreamReader reader = new StreamReader(memoryStream);
// Read MemoryStream object to a string.
memoryStreamValue = reader.ReadToEnd();
// Close StreamReader obj.
reader.Close();
仅使用方法 Convert.ToBase64String
Only use method Convert.ToBase64String
不知道为什么没有人回答这个问题:
Not sure why nobody answered this:
我需要与需要在其上写入流的类集成:
我创建一个简单的类,可以帮助减少多次使用的代码行:
然后您可以用一行代码替换示例
I need to integrate with a class that need a Stream to Write on it:
I create a simple class that can help to reduce lines of code for multiples use:
then you can replace the sample with a single line of code
Brian 的答案的稍微修改版本允许对读取开始进行可选管理,这似乎是最简单的方法。 可能不是最有效的,但易于理解和使用。
A slightly modified version of Brian's answer allows optional management of read start, This seems to be the easiest method. probably not the most efficient, but easy to understand and use.
为什么不在 MemoryStream 类型上创建一个很好的扩展方法呢?
当然,将这些方法与标准方法结合使用时要小心。 :) ...如果你这样做,你将需要使用方便的streamLock来实现并发。
Why not make a nice extension method on the MemoryStream type?
Of course, be careful when using these methods in conjunction with the standard ones. :) ...you'll need to use that handy streamLock if you do, for concurrency.
此示例演示了如何从 MemoryStream 读取字符串,其中我使用了序列化(使用 DataContractJsonSerializer),将字符串从某个服务器传递到客户端,然后如何从作为参数传递的字符串恢复 MemoryStream,然后,反序列化 MemoryStream。
我使用了不同帖子的部分内容来执行此示例。
希望这有帮助。
This sample shows how to read a string from a MemoryStream, in which I've used a serialization (using DataContractJsonSerializer), pass the string from some server to client, and then, how to recover the MemoryStream from the string passed as parameter, then, deserialize the MemoryStream.
I've used parts of different posts to perform this sample.
Hope that this helps.
在这种情况下,如果您确实想以简单的方式使用
MemoryStream
中的ReadToEnd
方法,您可以使用此扩展方法来实现此目的:并且您可以在这边走:
In this case, if you really want to use
ReadToEnd
method inMemoryStream
in an easy way, you can use this Extension Method to achieve this:And you can use this method in this way:
以前的解决方案在涉及编码的情况下不起作用。 这是 - 一种“现实生活” - 示例如何正确执行此操作......
Previous solutions wouldn't work in cases where encoding is involved. Here is - kind of a "real life" - example how to do this properly...
使用 StreamReader 将 MemoryStream 转换为字符串。
Using a StreamReader to convert the MemoryStream to a String.
使用 StreamReader,然后您可以使用 < a href="http://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend.aspx" rel="nofollow noreferrer">ReadToEnd 返回字符串的方法。
use a StreamReader, then you can use the ReadToEnd method that returns a string.
您还可以使用
I don't think this is less effective, but I won't swear to it。 它还允许您选择不同的编码,而使用 StreamReader 则必须将其指定为参数。
You can also use
I don't think this is less efficient, but I couldn't swear to it. It also lets you choose a different encoding, whereas using a StreamReader you'd have to specify that as a parameter.
此示例演示如何读取字符串并将其写入 MemoryStream。
This sample shows how to read and write a string to a MemoryStream.