How to read all text from a byte [] file?

I have a text file as a byte [].

I can not save the file anywhere.

I would like to read all the lines / text from this "file".

Can someone point me in the right direction, how can I read all the text from byte [] in C #?

Thanks!

+7
source share
2 answers

I would create a MemoryStream and instantiate a StreamReader with this: ie:

 var stream = new StreamReader(new MemoryStream(byteArray)); 

Then get the string text at a time with:

 stream.readLine(); 

Or the full file using:

 stream.readToEnd(); 
+19
source

Another possible solution using Encoding :

 Encoding.Default.GetString(byteArray); 

You can optionally split it to get the lines:

 Encoding.Default.GetString(byteArray).Split('\n'); 

You can also select a specific encoding, such as UTF-8, instead of using Default .

0
source

All Articles