how to retrieve the encoding of a csv file in c#.net?
I need to get the encoding type of a csv file and how can i do this in c#.net..
My code to avoid Byte Order Mapping(BMO) added during UTF8 encoding is as follows:
public static void SaveAsUTF8WithoutByteOrderMark(string fileName, Encoding encoding)
{
开发者_Python百科 if (fileName == null)
throw new ArgumentNullException("fileName");
if (encoding == null)
{
encoding = Encoding.Default;
}
File.WriteAllText(fileName, File.ReadAllText(fileName, encoding), new UTF8Encoding(false));
}
But any one please tell me how i can find the encoding of a csv file in C#.net..
There's an example of a simple class that will detect the encoding here (which doesn't just check for BOM
).
I would recommend CharsetDetector/UTF-unknown to find the encoding of a csv file. It's a Charset detector build in C# - .NET 5, .NET Core 2-3, .NET standard 1-2 & .NET 4+.
Detect character set for files, streams and other bytes.
This package is based on Ude and since version 2 also on uchardet, which are ports of the Mozilla Universal Charset Detector.
// Detect from File (NET standard 1.3+ or .NET 4+)
DetectionResult result = CharsetDetector.DetectFromFile("path/to/file.txt"); // or pass FileInfo
// Get the best Detection
DetectionDetail resultDetected = results.Detected;
// Get the alias of the found encoding
string encodingName = resultDetected.EncodingName;
// Get the System.Text.Encoding of the found encoding (can be null if not available)
Encoding encoding = resultDetected.Encoding;
Additional, here is a Python character encoding detector: Chardet: The Universal Character Encoding Detector
精彩评论