Stream into String

  • Thread starter Chad Z. Hower aka Kudzu
  • 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
 
J

Jon Skeet [C# MVP]

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.
 
C

Chad Z. Hower aka Kudzu

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
 
J

Jon Skeet [C# MVP]

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.
 
J

Jon Skeet [C# MVP]

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();
 
C

Chad Z. Hower aka Kudzu

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
 
P

Paul Hetherington

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()
 

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

Similar Threads

Indy for Visual Studio Developers 3
VB and implicit conversions 36
What does this do? Object[0] 12
"What is better" question ... 6
RegASM 7
dotnet.general 8
Regenerating a Typed DS 2
Formatting databound values 7

Top