MemoryStream to String question

  • Thread starter Thread starter Just Me
  • Start date Start date
J

Just Me

Does anybody have some sample code of converting a MemoryStream into a
String? I am having trouble doing this simple thing because I am tired and
under pressure. A code snippet would surly help. Thank you.
 
Just Me,

You can just pass the memory stream to the constructor of the
StreamReader class, and then call the ReadToEnd method, like so:

public function GetStringFromStream(Stream stream)
{
// Create a stream reader.
using (StreamReader reader = new StreamReader(stream))
{
// Just read to the end.
return reader.ReadToEnd();
}
}

If you need to set the encoding for the content in the stream, you can
pass an Encoding instance to the StreamReader constructor.

Hope this helps.
 
Nicholas Paldino said:
Just Me,

public function GetStringFromStream(Stream stream)

function? You forgot

using function = System.String; // I'm evil

Or are you stuck in Delphi- or JavaScript-mode, Nicholas?
Amagad, don't tell us you've been fooling around with C again. You Naught
Naughty Boy! =)

Happy coding
- Michael S
 
DelphiApp.cpp


#include "stdafx.h"
#define begin {
#define end }
#define WriteLn Console::WriteLine
#define program int
#using <mscorlib.dll>
using namespace System;

program _tmain()
begin
WriteLn("Hello World");
end
 
Back
Top