What file types can use System.IO.File.ReadAllLines()

Viewed 888

I'm creating a program that can convert different files types to pdf.

After creating a .txt to .pdf converter, which uses System.IO.File.ReadAllLines()

I realized that I could use that same converter for .csv, which left me wondering what other file types I could theoretically support because of the ReadAllLines approach.

2 Answers

Since ReadAllLines() reads the lines as text, you can use it for any text-based file type. There is no comprehensive list of "types" of files in this category (new file types are invented all the time), but most of them are probably going to be files intended for use as code (.cs, .java, etc.), or as structured data which is often used to transfer data between applications (.xml, .json, etc.).

You could theoretically call the method for other (binary) files, but you'd end up with a bunch of useless gobbledegook.

Saying that ReadAllLines() simply tryies to read the file as text might be confusing as another question might rise about what we mean by saying "read as text". Moreover, it also tries to detect encoding as well... So, let's avoid answering that way...

The short answer to the question of "What types are supported?" is simple:
Any type

Reason:
No matter the file is text file or even binary. What this method does is simply reading the bytes until finding line break or carriage return characters ('\r','\n','\r\n'). And as soon as it finds any, assumes that all the stuff before that was a line, and then continues reading the file by seeking for the next line break.

So in case of csv-s which worked on your side the reason is the same. That csv file had line breaks inside. Moreover even in case of having binary file, this function will return result (though it might be quite unuseful) just because it found some linebreaks or carriage return characters inside it. If no - then it would return whole binary data as a single-item array of strings.

Here are some more details taken from documentation just in case it's needed:

This method opens a file, reads each line of the file, then adds each line as an element of a string array. It then closes the file. A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed. The resulting string does not contain the terminating carriage return and/or line feed.

This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected.

Related