C # Get file encoding

Source: Internet
Author: User

/// <Summary>
/// Detects character encoding classes
/// <Seealso cref = "System. IO. Stream"/>
/// <Seealso cref = "System. Uri"/>
/// <Seealso cref = "System. IO. FileInfo"/>
/// </Summary>
/// <Remarks>
/// <! [CDATA [
/// <Strong> FileEncoder </strong> is used to detect <see cref = "Uri"/> and <see cref = "System. IO. fileInfo "/>, <see cref =" sbyte "/> encoding of byte arrays.
/// Create By lion <br/>
/// <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 [0, 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>
/// Initialize the <see cref = "IdentifyEncoding"/> instance
/// </Summary>
Public FileEncoder ()
{
Initialize_Frequencies ();
}

# Region GetEncodingName .....

/// <Summary>
/// Determine the encoding type from the specified <see cref = "Uri"/>
/// </Summary>
/// <Param name = "testurl"> <see cref = "Uri"/> </param>
/// <Returns> return encoding type ("GB2312", "GBK", "HZ", "Big5", "CNS 11643", "ISO 2022CN", "UTF-8 ", "Unicode", "ASCII", "OTHER") </returns>
/// <Example>
/// The following example shows how to call the <see cref = "GetEncodingName"/> method:
/// <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 [2, 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>
/// Determine the encoding type from the specified <see cref = "System. IO. FileInfo"/>
/// </Summary>
/// <Param name = "testfile"> <see cref = "System. IO. FileInfo"/> </param>
/// <Returns> return encoding type ("GB2312", "GBK", "HZ", "Big5", "CNS 11643", "ISO 2022CN", "UTF-8 ", "Unicode", "ASCII", "OTHER") </returns>
/// <Example>
/// The following example shows how to call the <see cref = "GetEncodingName"/> method:
/// <Code>
/// IdentifyEncoding ide = new IdentifyEncoding ();
/// Response. Write (ide. GetEncodingName (new System. IO. FileInfo (@ "C: est.txt ")));
& N

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.