vb.net判断文件编码 vb判断文件存在
VB 如何判断txt文件的编码格式
哈哈!一周前我刚刚遇到类似问题 慷慨把代码给你吧!!顺便贴上所有格式转UTF8的代码
成都创新互联公司于2013年创立,先为玉泉等服务建站,玉泉等地企业,进行企业商务咨询服务。为玉泉企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
(我本来是给XML读取用的 MSXML太挑剔了 只读UTF-8格式- - 不过转UTF-8的还是有点问题 记事本打开看上去读取没有问题 但是用MSXML读取依然有点问题。)
原理:根据文件前两个字符判断
注意 放在一个模块里面。
使用:GetEncoding(文件路径),返回一个Encoding枚举体(仿.net)
例如:
Dim ReturnEncoding As Encoding
ReturnEncoding = GetEncoding("C:\1.txt")
If ReturnEncoding = UTF8 Then
'UTF处理
ElseIf ReturnEncoding = Unicode Then
'Unicode
Elseif ReturnEncoding = UnicodeBigEndian Then
'Unicode-BE处理
Else
'ANSI处理
End If
========模块Encoding.bas==========
Option Explicit
Public Enum Encoding
ANSI
Unicode
UnicodeBigEndian
UTF8
End Enum
Public Function GetEncoding(FileName As String) As Encoding
On Error GoTo Err
Dim fBytes(1) As Byte, freeNum As Integer
freeNum = FreeFile
Open FileName For Binary Access Read As #freeNum
Get #freeNum, , fBytes(0)
Get #freeNum, , fBytes(1)
Close #freeNum
If fBytes(0) = HFF And fBytes(1) = HFE Then GetEncoding = Unicode
If fBytes(0) = HFE And fBytes(1) = HFF Then GetEncoding = UnicodeBigEndian
If fBytes(0) = HEF And fBytes(1) = HBB Then GetEncoding = UTF8
Err:
End Function
Public Sub FileToUTF8(FileName As String)
Dim fBytes() As Byte, uniString As String, freeNum As Integer
Dim ADO_Stream As Object
freeNum = FreeFile
ReDim fBytes(FileLen(FileName))
Open FileName For Binary Access Read As #freeNum
Get #freeNum, , fBytes
Close #freeNum
uniString = StrConv(fBytes, vbUnicode)
Set ADO_Stream = CreateObject("ADODB.Stream")
With ADO_Stream
.Type = 2
.Mode = 3
.Charset = "utf-8"
.open
.WriteText uniString
.SaveToFile FileName, 2
.Close
End With
Set ADO_Stream = Nothing
End Sub
vb.net 二进制读取文件
VB.NET打开二进制文件用fileopen完成,打开二进制文件的形式为:openmode.binary
读取二进制文件用的是fileget方法,写入二进制文件用的是fileput方法。
应用示例:将一批随机数保存在一个dat文件中,然后再将其提取到文本框中。
二进制文件的读写一批随机数的存取,程序为:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x, i, fn As Integer
Dim s As String = ""
fn = FreeFile()
FileOpen(fn, "d:\data.dat", OpenMode.Binary)
For i = 1 To 8
x = Int(Rnd() * 100)
s = s + Str(x)
FilePut(fn, x)
Next
FileClose(fn)
TextBox1.Text = s
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x, fn As Integer
Dim s As String = ""
fn = FreeFile()
FileOpen(fn, "d:\data.dat", OpenMode.Binary)
Do While Not EOF(fn)
FileGet(fn, x)
s = s + Str(x) + " "
Loop
FileClose(fn)
TextBox1.Text = s
End Sub
VB 怎么获取或判断txt文件的编码格式
哈哈!一周前我刚刚遇到类似问题 慷慨把代码给你吧!!顺便贴上所有格式转UTF8的代码
(我本来是给XML读取用的 MSXML太挑剔了 只读UTF-8格式- - 不过转UTF-8的还是有点问题 记事本打开看上去读取没有问题 但是用MSXML读取依然有点问题。)
原理:根据文件前两个字符判断
注意 放在一个模块里面。
使用:GetEncoding(文件路径),返回一个Encoding枚举体(仿.net)
例如:
Dim ReturnEncoding As Encoding
ReturnEncoding = GetEncoding("C:\1.txt")
If ReturnEncoding = UTF8 Then
'UTF处理
ElseIf ReturnEncoding = Unicode Then
'Unicode
Elseif ReturnEncoding = UnicodeBigEndian Then
'Unicode-BE处理
Else
'ANSI处理
End If
========模块Encoding.bas==========
Option Explicit
Public Enum Encoding
ANSI
Unicode
UnicodeBigEndian
UTF8
End Enum
Public Function GetEncoding(FileName As String) As Encoding
On Error GoTo Err
Dim fBytes(1) As Byte, freeNum As Integer
freeNum = FreeFile
Open FileName For Binary Access Read As #freeNum
Get #freeNum, , fBytes(0)
Get #freeNum, , fBytes(1)
Close #freeNum
If fBytes(0) = HFF And fBytes(1) = HFE Then GetEncoding = Unicode
If fBytes(0) = HFE And fBytes(1) = HFF Then GetEncoding = UnicodeBigEndian
If fBytes(0) = HEF And fBytes(1) = HBB Then GetEncoding = UTF8
Err:
End Function
Public Sub FileToUTF8(FileName As String)
Dim fBytes() As Byte, uniString As String, freeNum As Integer
Dim ADO_Stream As Object
freeNum = FreeFile
ReDim fBytes(FileLen(FileName))
Open FileName For Binary Access Read As #freeNum
Get #freeNum, , fBytes
Close #freeNum
uniString = StrConv(fBytes, vbUnicode)
Set ADO_Stream = CreateObject("ADODB.Stream")
With ADO_Stream
.Type = 2
.Mode = 3
.Charset = "utf-8"
.open
.WriteText uniString
.SaveToFile FileName, 2
.Close
End With
Set ADO_Stream = Nothing
End Sub
VB.NET如何获取任何格式文件的文字信息
Option Explicit
'mTextUTF.bas
'模块:UTF文本文件访问
'作者:zyl910
'版本:1.0
'日期:2006-1-23
'== 说明 ===================================================
'支持Unicode编码的文本文件读写。暂时支持ANSI、UTF-8、UTF-16LE、UTF-16BE这几种编码文本
'== 更新记录 ===============================================
'[V1.0] 2006-1-23
'1.支持最常见的ANSI、UTF-8、UTF-16LE、UTF-16BE这几种编码文本
'## 编译预处理常数 #########################################
'== 全局常数 ===============================================
'IncludeAPILib:引用了API库,此时不需要手动写API声明
'## API ####################################################
#If IncludeAPILib = 0 Then
'== File ===================================================
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Private Const INVALID_HANDLE_VALUE = -1
Private Const GENERIC_READ = H80000000
Private Const GENERIC_WRITE = H40000000
Private Const FILE_SHARE_READ = H1
Private Const FILE_SHARE_WRITE = H2
Private Const Create_NEW = 1
Private Const Create_ALWAYS = 2
Private Const OPEN_EXISTING = 3
Private Const OPEN_ALWAYS = 4
Private Const TRUNCATE_EXISTING = 5
Private Const FILE_ATTRIBUTE_NORMAL = H80
Private Const FILE_BEGIN = 0
Private Const FILE_CURRENT = 1
Private Const FILE_END = 2
'== Unicode ================================================
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByRef lpWideCharStr As Any, ByVal cchWideChar As Long) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpWideCharStr As Any, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByRef lpDefaultChar As Any, ByVal lpUsedDefaultChar As Long) As Long
Private Const CP_UTF8 As Long = 65001
#End If
'###########################################################
'Unicode编码格式
Public Enum UnicodeEncodeFormat
UEF_ANSI = 0 'ANSI+DBCS
UEF_UTF8 'UTF-8
UEF_UTF16LE 'UTF-16LE
UEF_UTF16BE 'UTF-16BE
UEF_UTF32LE 'UTF-32LE
UEF_UTF32BE 'UTF-32BE
UEF_Auto = -1 '自动识别编码
'隐藏项目
[_UEF_Min] = UEF_ANSI
[_UEF_Max] = UEF_UTF32BE
End Enum
'ANSI+DBCS方式的文本所使用的代码页。默认为0,表示使用系统当前代码页。可以利用该参数实现读取其他代码编码的文本,比如想在 简体中文平台下 读取 繁体中文平台生成的txt,就将它设为950
Public UEFCodePage As Long
'判断BOM
'返回值:BOM所占字节
'dwFirst:[in]文件最开始的4个字节
'fmt:[out]返回编码类型
Public Function UEFCheckBOM(ByVal dwFirst As Long, ByRef fmt As UnicodeEncodeFormat) As Long
If dwFirst = HFEFF Then
fmt = UEF_UTF32LE
UEFCheckBOM = 4
ElseIf dwFirst = HFFFE0000 Then
fmt = UEF_UTF32BE
UEFCheckBOM = 4
ElseIf (dwFirst And HFFFF) = HFEFF Then
fmt = UEF_UTF16LE
UEFCheckBOM = 2
ElseIf (dwFirst And HFFFF) = HFFFE Then
fmt = UEF_UTF16BE
UEFCheckBOM = 2
ElseIf (dwFirst And HFFFFFF) = HBFBBEF Then
fmt = UEF_UTF8
UEFCheckBOM = 3
Else
fmt = UEF_ANSI
UEFCheckBOM = 0
End If
End Function
'生成BOM
'返回值:BOM所占字节
'fmt:[in]编码类型
'dwFirst:[out]文件最开始的4个字节
Public Function UEFMakeBOM(ByVal fmt As UnicodeEncodeFormat, ByRef dwFirst As Long) As Long
Select Case fmt
Case UEF_UTF8
dwFirst = HBFBBEF
UEFMakeBOM = 3
Case UEF_UTF16LE
dwFirst = HFEFF
UEFMakeBOM = 2
Case UEF_UTF16BE
dwFirst = HFFFE
UEFMakeBOM = 2
Case UEF_UTF32LE
dwFirst = HFEFF
UEFMakeBOM = 4
Case UEF_UTF32BE
dwFirst = HFFFE0000
UEFMakeBOM = 4
Case Else
dwFirst = 0
UEFMakeBOM = 0
End Select
End Function
'判断文本文件的编码类型
'返回值:编码类型。文件无法打开时,返回UEF_Auto
'FileName:文件名
Public Function UEFCheckTextFileFormat(ByVal FileName As String) As UnicodeEncodeFormat
Dim hFile As Long
Dim dwFirst As Long
Dim nNumRead As Long
'打开文件
hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, ByVal 0)
If INVALID_HANDLE_VALUE = hFile Then '文件无法打开
UEFCheckTextFileFormat = UEF_Auto
Exit Function
End If
'判断BOM
dwFirst = 0
Call ReadFile(hFile, dwFirst, 4, nNumRead, ByVal 0)
nNumRead = UEFCheckBOM(dwFirst, UEFCheckTextFileFormat)
'Debug.Print nNumRead
'关闭文件
Call CloseHandle(hFile)
End Function
'读取文本文件
'返回值:读取的文本。返回vbNullString表示文件无法打开
'FileName:[in]文件名
'fmt:[in,out]使用何种文本编码格式来读取文本。为UEF_Auto时表示自动判断,且在fmt参数返回文本所用编码格式
Public Function UEFLoadTextFile(ByVal FileName As String, Optional ByRef fmt As UnicodeEncodeFormat = UEF_Auto) As String
Dim hFile As Long
Dim nFileSize As Long
Dim nNumRead As Long
Dim dwFirst As Long
Dim CurFmt As UnicodeEncodeFormat
Dim cbBOM As Long
Dim cbTextData As Long
Dim CurCP As Long
Dim byBuf() As Byte
Dim cchStr As Long
Dim I As Long
Dim byTemp As Byte
'判断fmt范围
If fmt UEF_Auto Then
If fmt [_UEF_Min] Or fmt [_UEF_Max] Then
GoTo FunEnd
End If
End If
'打开文件
hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, ByVal 0)
If INVALID_HANDLE_VALUE = hFile Then '文件无法打开
GoTo FunEnd
End If
'判断文件大小
nFileSize = GetFileSize(hFile, nNumRead)
If nNumRead 0 Then '超过4GB
GoTo FreeHandle
End If
If nFileSize 0 Then '超过2GB
GoTo FreeHandle
End If
'判断BOM
dwFirst = 0
Call ReadFile(hFile, dwFirst, 4, nNumRead, ByVal 0)
cbBOM = UEFCheckBOM(dwFirst, CurFmt)
'恢复文件指针
If fmt = UEF_Auto Then '自动判断
fmt = CurFmt
'cbBOM = cbBOM
Else '手动设置编码
If fmt = CurFmt Then '若编码相同,则忽略BOM标记
'cbBOM = cbBOM
Else '编码不同,那么都是数据
cbBOM = 0
End If
End If
Call SetFilePointer(hFile, cbBOM, ByVal 0, FILE_BEGIN)
cbTextData = nFileSize - cbBOM
'读取数据
UEFLoadTextFile = ""
Select Case fmt
Case UEF_ANSI, UEF_UTF8
'判断应使用的CodePage
CurCP = IIf(fmt = UEF_UTF8, CP_UTF8, UEFCodePage)
文章名称:vb.net判断文件编码 vb判断文件存在
文章地址:http://scjbc.cn/article/dosoppg.html