我刚刚学习了这个,来一段vb.net的代码,这是我从网上搜索到的。转化成了vb。使用方法是把这些代码保存成一个类。然后在另一个类中调用 : dim ed as encoding ed = encodingtype.getcodetype(fname)简单吧。具体代码如下:imports systemimports system.ioimports system.text'/ <summary>'/ 获取文件的编码格式'/ </summary>public class encodingtype '/ <summary> '/ 给定文件的路径,读取文件的二进制数据,判断文件的编码类型 '/ </summary> '/ <param name="file_name">文件路径</param> '/ <returns>文件的编码类型</returns> public overloads shared function getcodetype(byval file_name as string) as system.text.encoding dim fs as new filestream(file_name, filemode.open, fileaccess.read) dim r as encoding = getcodetype(fs) fs.close() return r end function 'gettype'/ <summary> '/ 通过给定的文件流,判断文件的编码类型 '/ </summary> '/ <param name="fs">文件流</param> '/ <returns>文件的编码类型</returns> public overloads shared function getcodetype(byval fs as filestream) as system.text.encoding dim [unicode]() as byte = {&hff, &hfe, &h41} dim unicodebig() as byte = {&hfe, &hff, &h0} dim utf8() as byte = {&hef, &hbb, &hbf} '带bom dim reval as encoding = encoding.default dim r as new binaryreader(fs, system.text.encoding.default) dim i as integer integer.tryparse(fs.length.tostring(), i) dim ss as byte() = r.readbytes(i) if **utf8bytes(ss) or (ss(0) = &hef and ss(1) = &hbb and ss(2) = &hbf) then reval = encoding.utf8 else if ss(0) = &hfe and ss(1) = &hff and ss(2) = &h0 then reval = encoding.bigendianunicode else if ss(0) = &hff and ss(1) = &hfe and ss(2) = &h41 then reval = encoding.unicode end if end if end if r.close() return reval end function 'gettype'/ <summary> '/ 判断是否是不带 bom 的 utf8 格式 '/ </summary> '/ <param name="data"></param> '/ <returns></returns> private shared function **utf8bytes(byval data() as byte) as boolean dim charbytecounter as integer = 1 '计算当前正分析的字符应还有的字节数 dim curbyte as byte '当前分析的字节. dim i as integer for i = 0 to data.length - 1 curbyte = data(i) if charbytecounter = 1 then if curbyte >= &h80 then '判断当前 while (curbyte <= 1 and &h80) <> 0 charbytecounter += 1 end while '标记位首位若为非0 则至少以2个1开始 如:110xxxxx...........1111110x if charbytecounter = 1 or charbytecounter > 6 then return false end if end if else '若是utf-8 此时第一位必须为1 if (curbyte and &hc0) <> &h80 then return false end if charbytecounter -= 1 end if next i if charbytecounter > 1 then throw new exception("非预期的byte格式") end if return true end function '**utf8bytesend class 'encodingtype 20210311