Finding out a file encoding

  • Thread starter Gaia C via .NET 247
  • 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
 
M

Morten Wennevik

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top