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
?
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 ) );
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.
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.
TextReader tr = new StreamReader(stream);
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. }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With