Possible Duplicate:
How can I detect the encoding/codepage of a text file
I have a ASP.NET MVC application. In my view I upload a text file and process it with a controller method with this signature
[HttpPost]
public ActionResult FromCSV(HttpPostedFileBase file, string platform)
I get a stream from the uploaded file as file.InputStream and read it using a standard StreamReader
using (var sr = new StreamReader(file.InputStream))
{
...
}
The problem is, that this only works for UTF text files. When I have a text file in Windows-1250, the characters get messed up. I can work with Windows-1250 encoded text files when I explicitly specify the encoding
using (var sr = new StreamReader(file.InputStream, Encoding.GetEncoding(1250)))
{
...
}
My problem is, that I need to support both UTF and Windows-1250 encoded files so I need a way to detect the encoding of the submitted file.
Trying to decode a file encoded in Windows-1250 as UTF-8 is extremely likely to cause an exception (or if not, the file is only using ASCII subset so it doesn’t matter what encoding is used to decode) with exception fallback, so you could do something like this: