程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#獲取文件編碼

C#獲取文件編碼

編輯: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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved