vb.net 判断文件编码的方法

前端之家收集整理的这篇文章主要介绍了vb.net 判断文件编码的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_1@ vb.net 判断文件编码的三种方法
@H_502_1@ 使用方法GetEncoding(文件名)
@H_502_1@ ' 读取文本文件内容
@H_502_1@ '
@H_502_1@ ' 文件
@H_502_1@ ' 文件编码
@H_502_1@ ' 文件内容
@H_502_1@
@H_502_1@ Public Function ReadFile(ByVal fileName As String) As String @H_502_1@ If File.Exists(fileName) Then @H_502_1@ Return File.ReadAllText(fileName,GetEncoding(fileName)) @H_502_1@ Else @H_502_1@ Return string.Empty @H_502_1@ End If @H_502_1@ End Function @H_502_1@ @H_502_1@ ' @H_502_1@ ' 判断文件编码类型 @H_502_1@ ' @H_502_1@ ' @H_502_1@ ' @H_502_1@ Public Overloads Shared Function GetEncoding(ByVal fileName As String) As Encoding @H_502_1@ Dim fs As FileStream = New FileStream(fileName,FileMode.Open,FileAccess.Read) @H_502_1@ Dim r As Encoding = GetEncoding(fs) @H_502_1@ fs.Close @H_502_1@ Return r @H_502_1@ End Function @H_502_1@ @H_502_1@ ' @H_502_1@ ' 判断文件流编码类型 @H_502_1@ ' @H_502_1@ ' @H_502_1@
@H_502_1@ Private Overloads Shared Function GetEncoding(ByVal fs As FileStream) As Encoding @H_502_1@ Dim r As BinaryReader = New BinaryReader(fs,System.Text.Encoding.Default) @H_502_1@ Dim ss() As Byte = r.ReadBytes(3) @H_502_1@ r.Close @H_502_1@ @H_502_1@ If (ss(0) >= 239) Then @H_502_1@ BF @H_502_1@ Return Encoding.UTF8 @H_502_1@ If ((ss(0) = 254) _ @H_502_1@ AndAlso (ss(1) = 255)) Then @H_502_1@ Return Encoding.BigEndianUnicode @H_502_1@ ElseIf ((ss(0) = 255) _ @H_502_1@ AndAlso (ss(1) = 254)) Then @H_502_1@ Return Encoding.Unicode @H_502_1@ Else @H_502_1@ Return Encoding.Default @H_502_1@ End If @H_502_1@ Else @H_502_1@ Return Encoding.Default @H_502_1@ End If @H_502_1@ End Function

猜你在找的VB相关文章