现有N多TXT文件,比如1.txt, 2.txt, 3.txt, 4.txt.....编码方式为ANSI、UTF-8 或 UTF-16,我想知道每个文件的编码方式是什么,现在的方法是记事本打开文件,然后选择“另存为”....求一种方法,可以判断出文件的encoding, 在线急求,先谢谢了
解决方案 »
- updatePanel中动态创建div把获取的字段和图片一起放到一个iframe里面 可是不出效果阿~大大们麻烦看下谢谢
- 用Linq查询XML文件的问题
- 如何把DATASET数据插入到数据库中?
- wpf富文本框编辑器怎么做?
- 开发了.net类库后,可能供winform程序调用,也可能供winService程序使用,在类库中如何判断,当前是哪一种应用在调用它?
- 如何实现mdi子窗体的位置固定不可改变
- 求助:逐行更新datatable,
- 难道咱们中国没有人能具体解答这个问题吗?
- 如何把内存中的一个bitmap图像转化成可以存入数据库的字节数组形势?
- c#
- 50分一本电子书,有的,赶快传我,就可以得到50分!
- 请问一个.net访问paradox的问题
using System.IO;
using System.Text;class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt"; try
{
if (File.Exists(path))
{
File.Delete(path);
} //Use an encoding other than the default (UTF8).
using (StreamWriter sw = new StreamWriter(path, false, new UnicodeEncoding()))
{
sw.WriteLine("This");
sw.WriteLine("is some text");
sw.WriteLine("to test");
sw.WriteLine("Reading");
} using (StreamReader sr = new StreamReader(path, true))
{
while (sr.Peek() >= 0)
{
Console.Write((char)sr.Read());
} //Test for the encoding after reading, or at least
//after the first read.
Console.WriteLine("The encoding used was {0}.", sr.CurrentEncoding);
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
{
Encoding result = null;
FileStream file = null;
try
{
file = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
if (file.CanSeek)
{
// get the bom, if there is one
byte[] bom = new byte[4];
file.Read(bom, 0, 4); // utf-8
if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
result = System.Text.Encoding.UTF8;
// ucs-2le, ucs-4le, ucs-16le, utf-16, ucs-2, ucs-4
else if ((bom[0] == 0xff && bom[1] == 0xfe) ||
(bom[0] == 0xfe && bom[1] == 0xff) ||
(bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff))
result = System.Text.Encoding.Unicode;
// else ascii
else
result = System.Text.Encoding.ASCII;
}
else
{
// can't detect, set to default
result = System.Text.Encoding.ASCII;
} return result;
}
finally
{
if (null != file) file.Close();
}
} }
{
Encoding result = null;
FileStream file = null;
try
{
file = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);if (file.CanSeek)
{
// get the bom, if there is one
byte[] bom = new byte[4];
file.Read(bom, 0, 4);// utf-8
if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
result = System.Text.Encoding.UTF8;
// ucs-2le, ucs-4le, ucs-16le, utf-16, ucs-2, ucs-4
else if ((bom[0] == 0xff && bom[1] == 0xfe) ||
(bom[0] == 0xfe && bom[1] == 0xff) ||
(bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff))
result = System.Text.Encoding.Unicode;
// else ascii
else
result = System.Text.Encoding.ASCII;
}
else
{
// can't detect, set to default
result = System.Text.Encoding.ASCII;
}return result;
}
finally
{
if (null != file) file.Close();
}
}}