现有N多TXT文件,比如1.txt, 2.txt, 3.txt, 4.txt.....编码方式为ANSI、UTF-8 或 UTF-16,我想知道每个文件的编码方式是什么,现在的方法是记事本打开文件,然后选择“另存为”....求一种方法,可以判断出文件的encoding, 在线急求,先谢谢了

解决方案 »

  1.   

    The following code example gets the encoding of the specified StreamReader object.using System;
    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());
            }
        }
    }
      

  2.   

    这个可以static Encoding GetEncoding(string filepath)
    {
    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();
    }
    } }
      

  3.   

    static Encoding GetEncoding(string filepath)
    {
    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();
    }
    }}
      

  4.   

    CupTea(一杯清茶) 的方法中,reader 的 currentEncoding 总是uft8,也就是默认的encoding...还是孟子老大的方法比较强;另外,感谢孟子老大出山,给足了小弟面子^_^