Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a TextReader from a Stream?

Tags:

c#

.net

io

I'm trying to read an embedded text file with System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resource); but it gives me a Stream. The embedded resource is a text file so, how can I turn this Stream into a TextReader?

like image 588
Juan Avatar asked Mar 17 '11 17:03

Juan


People also ask

How do you turn a stream into a string?

To convert a string to a stream you need to decide which encoding the bytes in the stream should have to represent that string - for example you can: MemoryStream mStrm= new MemoryStream( Encoding. UTF8. GetBytes( contents ) );

What is a stream reader?

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file. Important. This type implements the IDisposable interface.

What is TextReader C#?

TextReader in C# is used to read text or sequential series of characters from a text file. TextReader class is found under System.IO namespace. It is an abstract base class of StreamReader and StringReader which are used to read characters from stream and string respectively.


2 Answers

TextReader tr = new StreamReader(stream); 
like image 179
Teo Klestrup Röijezon Avatar answered Oct 24 '22 06:10

Teo Klestrup Röijezon


A StreamReader is a subclass of TextReader, so you will be able to do:

using(var stream = System.Reflection.Assembly.GetExecutingAssembly().     GetManifestResourceStream(resource)) using(var reader = new StreamReader(stream)) {     // Use reader. } 
like image 28
driis Avatar answered Oct 24 '22 08:10

driis