By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

Stream into String

P: n/a
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

Jul 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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

Jul 21 '05 #3

P: n/a
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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.


No, you get a Stream, and construct a StreamReader *from* that Stream.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP**********************@msnews.microsoft.com :
No, you get a Stream, and construct a StreamReader *from* that Stream.


Is that really any cleaner or less code than I already have though?
--
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

Jul 21 '05 #5

P: n/a
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
No, you get a Stream, and construct a StreamReader *from* that Stream.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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

Jul 21 '05 #7

P: n/a
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()

"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in message
news:Xn******************@127.0.0.1...
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
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

Jul 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.