I have this text file what contains different fields. Some fields may contain binary data. I need to get all the data in the file but right now when using StreamReader then it wont read the binary data block and data what comes after that. What would be the best solution to solve this problem?
Example:
field1|field2|some binary data here|field3
Right now i read in the file like this:
public static string _fileToBuffer(string Filename)
{
if (!File.Exists(Filename)) throw new ArgumentNullException(Filename, "Template file does not exist");
StreamReader reader = new StreamReader(Filename, Encoding.Default, true);
string fileBuffer = reader.ReadToEnd();
reader.Close();
return fileBuffer;
}
EDIT: I know the start and end positions of the binary fields.
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. This type implements the IDisposable interface.
To read from a binary file Use the ReadAllBytes method, which returns the contents of a file as a byte array.
C# StreamReader class is used to read string from the stream. It inherits TextReader class. It provides Read() and ReadLine() methods to read data from the stream.
The BinaryReader class provides methods that simplify reading primitive data types from a stream. For example, you can use the ReadBoolean method to read the next byte as a Boolean value and advance the current position in the stream by one byte. The class includes read methods that support different data types.
use BinaryReader
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