Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with reading file in C#

Tags:

c#

encoding

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.

like image 327
Anton Kandybo Avatar asked Mar 20 '26 19:03

Anton Kandybo


2 Answers

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.

like image 60
Jon Skeet Avatar answered Mar 22 '26 10:03

Jon Skeet


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

like image 26
Anton Semenov Avatar answered Mar 22 '26 09:03

Anton Semenov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!