Problem about StreamReader and StreamWriter

R

R.L.

See the code below, var 'content ' is suppose to be "Hello!", not "". Who
knows why? Thanks
----------------------------------------
string text = "hello!";

MemoryStream stream = new MemoryStream();

StreamWriter streamWriter = new StreamWriter(stream, Encoding.ASCII);

streamWriter.Write(text);

//streamWriter.Flush();

StreamReader streamReader = new StreamReader(stream);

string content = "";

try

{

content = streamReader.ReadToEnd();

}//content =""

finally

{

streamReader.Close();

streamWriter.Close();

stream.Close();

}
 
M

Morten Wennevik

Hi RL,

Well, you have two errors in your code. Uncomment the StreamWriter.Flush
and reset the MemoryStream position which will be at the end of the text
when you write to it.

StreamReader.BaseStream.Position = 0;
 

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

Top