/// <Summary>
/// C # automatically determines the encoding function when reading a file
/// </Summary>
/// <Param name = "file_name"> Determine the physical path of the encoded file
For example " C: \ file \ file.txt " </ Param >
/// <Returns> </returns>
Public Static System. Text. Encoding GetType ( String File_name)
{
Filestream FS = New Filestream (file_name, filemode. Open,
fileaccess. read);
system. text. encoding r = GetType (FS );
FS. close ();
return r;
}
Public static system. text. encoding GetType (filestream FS)
{< br> /* byte [] Unicode = new byte [] {0xff, 0xfe };
byte [] unicodebig = new byte [] {0xfe, 0xff};
byte [] utf8 = new byte [] {0xef, 0xbb, 0xbf }; */
Binaryreader R= NewBinaryreader (FS,
System. Text. encoding. Default );
Byte [] SS = R. readbytes ( 4 );
R. Close ();
// Encoding type coding = encoding type. ASCII;
If (Ss [ 0 ] <= 0xef )
{
If (Ss [ 0 ] = 0xef && Ss [ 1 ] = 0xbb && Ss [ 2 ] = 0xbf )
{
Return System. Text. encoding. utf8;
}
Else If (Ss [ 0 ] = 0xfe && Ss [ 1 ] = 0xff )
{
Return System. Text. encoding. bigendianunicode;
}
Else If (Ss [ 0 ] = 0xff && Ss [ 1 ] = 0xfe )
{
Return System. Text. encoding. Unicode;
}
Else
{
Return System. Text. encoding. default;
}
}
Else
{
Return system. Text. encoding. Default;
}< BR >}