A memory stream is a kind of stream that has several methods to access and store data in memory.
Questions tagged [memorystream]
1242 questions
601
votes
12 answers
How do you get a string from a MemoryStream?
If I am given a MemoryStream that I know has been populated with a String, how do I get a String back out?

Brian
- 37,399
- 24
- 94
- 109
374
votes
9 answers
Save and load MemoryStream to/from a file
I am serializing an structure into a MemoryStream and I want to save and load the serialized structure.
So, How to Save a MemoryStream into a file and also load it back from file?

Mahdi Ghiasi
- 14,873
- 19
- 71
- 119
133
votes
9 answers
Attach a file from MemoryStream to a MailMessage in C#
I am writing a program to attach a file to email. Currently I am saving file using FileStream into disk, and then I use
System.Net.Mail.MailMessage.Attachments.Add(
new System.Net.Mail.Attachment("file name"));
I do not want to store file in…

Zain Ali
- 15,535
- 14
- 95
- 108
130
votes
12 answers
Is a memory leak created if a MemoryStream in .NET is not closed?
I have the following code:
MemoryStream foo(){
MemoryStream ms = new MemoryStream();
// write stuff to ms
return ms;
}
void bar(){
MemoryStream ms2 = foo();
// do stuff with ms2
return;
}
Is there any chance that the…

Coderer
- 25,844
- 28
- 99
- 154
112
votes
7 answers
MemoryStream - Cannot access a closed Stream
Hi why using (var sw = new StreamWriter(ms)) returns Cannot access a closed Stream exception. Memory Stream is on top of this code.
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(ms))
{
…

Arbejdsglæde
- 13,670
- 26
- 78
- 144
88
votes
9 answers
How to get a MemoryStream from a Stream in .NET?
I have the following constructor method which opens a MemoryStream from a file path:
MemoryStream _ms;
public MyClass(string filePath)
{
byte[] docBytes = File.ReadAllBytes(filePath);
_ms = new MemoryStream();
_ms.Write(docBytes, 0,…

fearofawhackplanet
- 52,166
- 53
- 160
- 253
84
votes
8 answers
difference between memory stream and filestream
During the serialization we can use either memory stream or file stream.
What is the basic difference between these two? What does memory stream mean?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using…

Raghav55
- 3,055
- 6
- 28
- 38
64
votes
5 answers
Reset or Clear .NET MemoryStream
The .NET MemoryStream does not appear to have a .Reset or .Clear method.
I was thinking of using the following code to accomplish this:
ms.Seek(0, IO.SeekOrigin.Begin)
ms.SetLength(0)
What is the proper way to clear or reset an existing .NET…
user113476
59
votes
4 answers
Reading from memory stream to string
I am trying to write an object to an Xml string and take that string and save it to a DB. But first I need to get the string...
private static readonly Encoding LocalEncoding = Encoding.UTF8;
public static string SaveToString (T…

tigerswithguitars
- 2,497
- 1
- 31
- 53
57
votes
4 answers
Serializing/deserializing with memory stream
I'm having an issue with serializing using memory stream. Here is my code:
///
/// serializes the given object into memory stream
///
/// the object to be serialized
/// The serialized…

gng
- 715
- 1
- 5
- 13
49
votes
3 answers
Copy MemoryStream to FileStream and save the file?
I don't understand what I'm doing wrong here. I generate couple of memory streams and in debug-mode I see that they are populated. But when I try to copy MemoryStream to FileStream in order to save the file fileStream is not populated and file is…

Stan
- 25,744
- 53
- 164
- 242
47
votes
3 answers
Writing to MemoryStream with StreamWriter returns empty
I am not sure what I am doing wrong, have seen a lot of examples, but can't seem to get this working.
public static Stream Foo()
{
var memStream = new MemoryStream();
var streamWriter = new StreamWriter(memStream);
for (int i = 0; i <…

jsmith
- 7,198
- 6
- 42
- 59
45
votes
9 answers
Name cannot begin with the ' ' character
I'm parsing some XML in C#. I'm getting it from a database, and so converting it to a MemoryStream before reading it with an XmlTextReader. The problem is that I get this error: Name cannot begin with the ' ' character, hexadecimal value 0x20. Line…

Brian Hicks
- 6,213
- 8
- 51
- 77
45
votes
7 answers
When is GetBuffer() on MemoryStream ever useful?
I've known that GetBuffer() on a MemoryStream in C#/.NET has to be used with care, because, as the docs describe here, there can be unused bytes at the end, so you have to be sure to look only at the first MemoryStream.Length bytes in the…

aggieNick02
- 2,557
- 2
- 23
- 36
45
votes
2 answers
MemoryStream in Using Statement - Do I need to call close()
When using a memory stream in a using statement do I need to call close? For instance is ms.Close() needed here?
using (MemoryStream ms = new MemoryStream(byteArray))
{
// stuff
ms.Close();
}

AJM
- 32,054
- 48
- 155
- 243