Problem reading a file in C #

I have a cp866 encoded file. In some places, the file contains the character 0 in hexadecimal code. When I try to read this file with File.ReadAllText () or streamReader.Read (), it stops reading the file in this character. How to solve this problem?
[UPDATE]
I think the character 0x0 means the end of the file.

+3
source share
2 answers

Hmmm. I think you should read the file as a binary, not as a text file.

+1
source

Are you looking for something like this?

Encoding encoding = Encoding.GetEncoding(866);
string text = File.ReadAllText("foo.txt", encoding);

Suppose you need a code page of 866.

I don’t know enough about CP866 to find out if it will usually contain 0 bytes ... but if your text file is valid CP866, this should read it.

+4
source

Source: https://habr.com/ru/post/1794650/


All Articles