开发者

Auto encoding detect in C# [duplicate]

开发者 https://www.devze.com 2023-01-16 14:40 出处:网络
This question already has answers here: Closed 12 years a开发者_StackOverflow中文版go. Possible Duplicate:
This question already has answers here: Closed 12 years a开发者_StackOverflow中文版go.

Possible Duplicate:

Determine a string's encoding in C#

Many text editorsr (like Notepad++) can detect encoding of arbitrary file. Can I detect encodoing of file in C#?


A StreamReader will try to automatically detect the encoding of a file if there's a BOM when trying to read:

public class Program
{
    static void Main(string[] args)
    {
        using (var reader = new StreamReader("foo.txt"))
        {
            // Make sure you read from the file or it won't be able
            // to guess the encoding
            var file = reader.ReadToEnd();
            Console.WriteLine(reader.CurrentEncoding);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号