How can I safely detect file encoding of a file submitted in .net?
You can't.
Excel's "CSV" saving comes out in the machine's ANSI code page, and "CSV (MS-DOS)" comes out in the OEM code page. Both these encodings vary from machine to machine and they're never anything useful like UTF-8 or UTF-16. (Indeed, on some East Asian machines, they may not even be fully ASCII-compatible.)
You might be able take a guess based on heuristics. For example if França
is a common value in the documents you handle, you could detect its common encodings:
F r a n ç a
Code page 1252 (ANSI on Western European machines): 46 72 61 6e e7 61
Code page 850 (OEM on Western European Machines): 46 72 61 6e 87 61
If you don't have any constant patterns like that the best you can do is arbitrary guessing (see this question). Either way it hardly qualifies as 'safe'.
CSV as a format does not have a mechanism for declaring encoding, and there isn't a de-facto standard of just using UTF-8. So it can't really be used as a mechanism for transferring non-ASCII text with any degree of reliability.
An alternative you could look at would be to encourage your users to save from Excel as "Unicode text". This will give you a .txt file in the UTF-16LE encoding (Encoding.Unicode
in .NET terms), which you can easily detect from the BOM. The content is TSV, so same quoting rules as CSV but with tab separators.