MemoryStream instance length is 0 after stream has been written

  • Thread starter Thread starter Mikus Sleiners
  • Start date Start date
M

Mikus Sleiners

I'm trying to write new stream from string and i can't figure out why my
memory stream
instance is null after i have writen to it with stream writer.

Here is an example.

MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter();

writer.Write ("my string");

at this point i check what does my stream contain and it says .Length = 0
so why is that ? How can it be 0 if i've just written to it with writer ?
 
I'm trying to write new stream from string and i can't figure out why
my
memory stream
instance is null after i have writen to it with stream writer.
Here is an example.

MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter();
writer.Write ("my string");

at this point i check what does my stream contain and it says .Length
= 0 so why is that ? How can it be 0 if i've just written to it with
writer ?

Call writer.Flush();

Best Regards,
Dustin Campbell
Developer Express Inc.
 
Mikus said:
I'm trying to write new stream from string and i can't figure out why my
memory stream
instance is null after i have writen to it with stream writer.

Here is an example.

MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter();

writer.Write ("my string");

at this point i check what does my stream contain and it says .Length = 0
so why is that ? How can it be 0 if i've just written to it with writer ?

How does your StreamWriter know to write to your stream? You have not
specified the stream that the stream writer should write to. Also, you
should use the using pattern if possible:

MemoryStream stream = new MemoryStream();
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write("my string");
}

Notice that the memory stream was passed into the constructor of the
stream writer.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top