Stream into String

  • Thread starter Thread starter Chad Z. Hower aka Kudzu
  • Start date Start date
C

Chad Z. Hower aka Kudzu

I have a stream, I need to get it into a string. Is this the best way to do
it? Seems like a lot of junk just to get it into the string.

Forget the fact that its VB - its for inside a VB project

Dim LASCII As New ASCIIEncoding
Dim LResult As String
Dim LStream As New MemoryStream

<some stuff that loads into the stream>

LResult = LASCII.GetString(LStream.ToArray)
LStream.Close()


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
Chad Z. Hower aka Kudzu said:
I have a stream, I need to get it into a string. Is this the best way to do
it? Seems like a lot of junk just to get it into the string.

Forget the fact that its VB - its for inside a VB project

Dim LASCII As New ASCIIEncoding
Dim LResult As String
Dim LStream As New MemoryStream

<some stuff that loads into the stream>

LResult = LASCII.GetString(LStream.ToArray)
LStream.Close()

A better way is to use a StreamReader, which you give the encoding you
want to use when you construct it, eg Encoding.ASCII. Then you just
read characters/strings from the StreamReader instead of bytes/chunks
from the stream.
 
Jon Skeet said:
A better way is to use a StreamReader, which you give the encoding you
want to use when you construct it, eg Encoding.ASCII. Then you just
read characters/strings from the StreamReader instead of bytes/chunks
from the stream.

I cannot use a StreamReader - the "source" I have is a Stream and only
accepts Stream or descendants, which VS says StreamReader is not.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
Chad Z. Hower aka Kudzu said:
I cannot use a StreamReader - the "source" I have is a Stream and only
accepts Stream or descendants, which VS says StreamReader is not.

No, you get a Stream, and construct a StreamReader *from* that Stream.
 
Chad Z. Hower aka Kudzu said:
Is that really any cleaner or less code than I already have though?

Well, I certainly think so :)

Do you want to read the whole of the stream in as a string? If so, use
StringReader.ReadToEnd. Then it's as simple as:

StreamReader reader = new StreamReader (stream, Encoding.Whatever);
string text = reader.ReadToEnd();
 
Jon Skeet said:
Do you want to read the whole of the stream in as a string? If so, use
StringReader.ReadToEnd. Then it's as simple as:

StreamReader reader = new StreamReader (stream, Encoding.Whatever);
string text = reader.ReadToEnd();

Hmm. That looks a bit better.

Yes - If you look at the new Indy article you can see what I wanted to do.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
When you 'New' the streamreader one of the overloaded constructors is a
stream from which to read.

Dim myStream as New FileStream(...)
dim myReader as New StreamReader(myStream)
dim myText as String=myReader.ReadToEnd()
myReader.Flush()
myReader.Close()
 
Back
Top