如何猜测.NET中没有BOM的文件的编码? [英] How to guess the encoding of a file with no BOM in .NET?
问题描述
我在.NET中使用StreamReader类,如下所示:
I'm using the StreamReader class in .NET like this:
using( StreamReader reader = new StreamReader( "c:\somefile.html", true ) {
string filetext = reader.ReadToEnd();
}
当文件有一个BOM时,这个工作正常。我遇到了一个没有BOM的文件的麻烦,基本上我有乱码当我指定Encoding.Unicode它工作正常,例如:
This works fine when the file has a BOM. I ran into trouble with a file with no BOM .. basically I got gibberish. When I specified Encoding.Unicode it worked fine, eg:
using( StreamReader reader = new StreamReader( "c:\somefile.html", Encoding.Unicode, false ) {
string filetext = reader.ReadToEnd();
}
文件内容到一个字符串,所以人们通常如何处理这个?我知道有没有解决方案,将工作100%的时间,但我想提高我的赔率..有明显的软件在那里试图猜测例如,记事本,浏览器等)。在.NET框架中有一个方法来猜测我吗?任何人都有一些代码,他们想分享?
So, I need to get the file contents into a string. So how do people usually handle this? I know there's no solution that will work 100% of the time, but I'd like to improve my odds .. there is obviously software out there that tries to guess (eg, notepad, browsers, etc). Is there a method in the .NET framework that will guess for me? Does anyone have some code they'd like to share?
更多背景:此与我的很相似,但我在.NET的土地。这个问题让我看到一个博客列出了各种编码检测库,但没有.NET。
More background: This question is pretty much the same as mine, but I'm in .NET land. That question led me to a blog listing various encoding detection libraries, but none are in .NET
推荐答案
Libary
http://www.codeproject.com/KB/recipes/DetectEncoding.aspx
这篇关于如何猜测.NET中没有BOM的文件的编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!