StreamReader encoding autodetect, and fallback default problems, plz help
Hi Im having some problem to get StreamReader behave "correct" (?)
Take a look:
fileStream =
newStreamReader(fileInfo.OpenRead(),Encoding.GetEncoding(1252), true);... As I understand this will check first bytes of file for UTF8 mask and if there isn't it will fallback to default encoding 1252 ?
But no metter which file I oppen I allways getfileStream.CurrentEncoding = 1252. I Tryed with many different files and chechek that
EF BB BF in place, but no UTF8 returned from CurrentEncoding.
Thanks for helping !
[1078 byte] By [
zubziro] at [2007-12-23]
Thank tou wery much for your answer.
You are correct i'm trying to use BOM autodetect, the problem is system needs to process two types of text files
Windows1252 encoding (has no BOM) and UTF8, I need read the file get som information from it and write to new file witch must be in same code standart.
When I use: StreamReader(String, true) I always get UTF8 enconding becouse when I open Windows1252 encoded file and there is no BOM and StreamReader defaults to UTF8 (So I allways get UTF8 no mether UTF8 or Windows1252 file).
The problem begins when I need to create new file and store readed information, couse then I don't know for shure which code standart was used by original file. So new created file is allways UTF8 standard when I read property
fileStream.CurrentEncoding And try to use it as parameter to stream writer.
Becouse of that I tryed to Use:
StreamReader(fileInfo.OpenRead(), Encoding.GetEncoding(1252), true);
As I understand when I create instanse with this constructor it should default to cp1252 when no BOM is found, but strange thing now I allways get cp1252 no mether if file is 1252 or UTF8 !