pete_roth wrote:That should get the number of characters for assuming your loop is reading properly.
Well, no, as written above, that will get the length of the last read line in the file -- which is only going to give you the full length of the file in the pathological case where there's no CR/LF chars -- ie, it's just one long line.
Of course you could keep a running accumulation as you loop through the lines of the file, but that's unnecessary if you're just trying to grab the length. Both the
filestream class and the
FileInfo class have .Length properties. Note also that the StreamReader class has a
ReadtoEnd method that will suck in the entire file without requiring a loop -- but again, that's only necessary if you're planning to do something with the contents of the file. If you just want to know how big it is, the FileInfo Length property will give that to you.
(I am ignoring a technical issue here wrt bytes vs characters -- without knowing the encoding of the text file, it's not really possible to know how many
characters there are in the file. If it's ASCII/ANSI, then every byte is a character; if it's unicode, then every two bytes define a character (approximately); but it could be one of several other encodings, including compressed ones. For the most part, you shouldn't have to worry about that.)