当前位置:编程学习 > C#/ASP.NET >>

C#获取文件编码

/// <summary>
    /// 检测字符编码的类
    /// <seealso cref="System.IO.Stream"/>
    /// <seealso cref="System.Uri"/>
    /// <seealso cref="System.IO.FileInfo"/>
    /// </summary>
    /// <remarks>
    /// <![CDATA[
    /// <strong>FileEncoder</strong> 用来检测 <see cref="Uri"/>,<see cref="System.IO.FileInfo"/>,<see cref="sbyte"/> 字节数组的编码.
    /// Create By lion  <br/>
    /// 2005-02-21 22:00  <br/>
    /// Support .Net Framework v1.1.4322 <br/>
    /// WebSite:www.lionsky.net(lion-a AT sohu.com) <br/>
    /// ]]>
    /// </remarks>
    public class FileEncoder
    {
        #region Fields.....

        // Frequency tables to hold the GB, Big5, and EUC-TW character
        // frequencies
        internal static int[][] GBFreq = new int[94][];
        internal static int[][] GBKFreq = new int[126][];
        internal static int[][] Big5Freq = new int[94][];
        internal static int[][] EUC_TWFreq = new int[94][];

        internal static string[] nicename = new string[]
   {
    "GB2312", "GBK", "HZ", "Big5", "CNS 11643"
    , "ISO 2022CN", "UTF-8", "Unicode", "ASCII", "OTHER"
   };

        #endregion

        #region Methods.....

        /// <summary>
        /// 初始化 <see cref="IdentifyEncoding"/> 的实例
        /// </summary>
        public FileEncoder()
        {
            Initialize_Frequencies();
        }

        #region GetEncodingName.....

        /// <summary>
        /// 从指定的 <see cref="Uri"/> 中判断编码类型
        /// </summary>
        /// <param name="testurl">要判断的 <see cref="Uri"/> </param>
        /// <returns>返回编码类型("GB2312", "GBK", "HZ", "Big5", "CNS 11643", "ISO 2022CN", "UTF-8", "Unicode", "ASCII", "OTHER")</returns>
        /// <example>
        /// 以下示例演示了如何调用 <see cref="GetEncodingName"/> 方法:
        /// <code>
        ///  IdentifyEncoding ide = new IdentifyEncoding();
        ///  Response.Write(ide.GetEncodingName(new Uri("http://china5.nikkeibp.co.jp/china/news/com/200307/pr_com200307170131.html"))); 
        /// </code>
        /// </example>
        public virtual string GetEncodingName(System.Uri testurl)
        {
            sbyte[] rawtext = new sbyte[1024];
            int bytesread = 0, byteoffset = 0;
            System.IO.Stream chinesestream;
            try
            {
                chinesestream = System.Net.WebRequest.Create(testurl.AbsoluteUri).GetResponse().GetResponseStream();
                while ((bytesread = ReadInput(chinesestream, ref rawtext, byteoffset, rawtext.Length - byteoffset)) > 0)
                {
                    byteoffset += bytesread;
                }
                chinesestream.Close();
            }
            catch (System.Exception e)
            {
                System.Console.Error.WriteLine("Error loading or using URL " + e.ToString());
            }
            return GetEncodingName(rawtext);
        }

        /// <summary>
        /// 从指定的 <see cref="System.IO.FileInfo"/> 中判断编码类型
        /// </summary>
        /// <param name="testfile">要判断的 <see cref="System.IO.FileInfo"/> </param>
        /// <returns>返回编码类型("GB2312", "GBK", "HZ", "Big5", "CNS 11643", "ISO 2022CN", "UTF-8", "Unicode", "ASCII", "OTHER")</returns>
        /// <example>
        /// 以下示例演示了如何调用 <see cref="GetEncodingName"/> 方法:
        /// <code>
        ///  IdentifyEncoding ide = new IdentifyEncoding();
        ///  Response.Write(ide.GetEncodingName(new System.IO.FileInfo(@"C: est.txt"))); 
     &n

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,