Anton Kandybo
Anton Kandybo

Reputation: 4098

Problem with reading file in C#

I have file in cp866 encoding. In some places it's file contains symbol 0 in hex code. When I try to read this file with File.ReadAllText() or streamReader.Read() it's stop reading file in this symbol. How to solve this problem?
[UPDATE]
I think that symbol 0x0 means end of file.

Upvotes: 3

Views: 1855

Answers (2)

Anton Semenov
Anton Semenov

Reputation: 6347

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

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503180

Are you looking for something like this?

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

That assuming you want code page 866.

I don't know enough about CP866 to know whether it would normally contain 0 bytes... but if your text file is valid CP866, the above should read it.

Upvotes: 4

Related Questions