本文介绍了C#中的自动编码检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
许多文字editorsr(如记事本++)可以检测任意文件的编码。我可以在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);
}
}
}
这篇关于C#中的自动编码检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!