471,594 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

MemoryStream instance length is 0 after stream has been written

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 ?
Nov 29 '06 #1
2 4484
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.
Nov 29 '06 #2

Mikus Sleiners wrote:
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

Nov 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Stingray | last post: by
7 posts views Thread by Donovan | last post: by
2 posts views Thread by Brian Mitchell | last post: by
13 posts views Thread by Don | last post: by
10 posts views Thread by Asaf | last post: by
7 posts views Thread by Maciej Oszutowski | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.