Finding out a file encoding

  • Thread starter Thread starter Gaia C via .NET 247
  • Start date Start date
G

Gaia C via .NET 247

Hi All,
How can i found out at what encoding the file was saved?
I tried usign GetPreamble(), but for this i should already create a stream which get an encoding...

StreamReader sr = new StreamReader(path);
sr.CurrentEncoding.GetPreamble();

any idea?

Thanks,
Gaia
 
Hi Gaia,

You should do

byte[] p = Encoding.Unicode.GetPreamble();
And then test the first bytes in your file against these bytes for each possible encoding.

This may work for unicode encoded files since I believe they add a mark to the file to specify endianess. However, you are not guaranteed to detect the file encoding this way because

1: The encoding may not have an identifying mark.
2: Your file may not have an identifying mark.

I don't think any ANSI encoded files have an identifying mark.
 
Back
Top