自动编码检测C# [英] Auto encoding detect in C#
本文介绍了自动编码检测C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
确定字符串的编码在C#中
许多文字editorsr(如记事本+ +)可以检测任意文件的编码。我能察觉文件encodoing在C#?
Many text editorsr (like Notepad++) can detect encoding of arbitrary file. Can I detect encodoing of file in C#?
推荐答案
一个的StreamReader < /一>将尝试自动检测一个文件的编码是否有试图读取时一个BOM:
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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文