VB可以Get 语句一次全部读入任何文件字节流。
Get 语句,将一个已打开的磁盘文件读入一个变量之中。
以下是读入文本文件的代码,显示于RichTextBox控件。
Private Sub Command1_Click()
Dim strWj As String
Dim aryContent() As Byte
CommonDialog1.CancelError = True ' 设置“CancelError”为 True
On Error GoTo ErrHandler
CommonDialog1.Flags = cdlOFNHideReadOnly ' 设置标志
' 设置过滤器
CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & "(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
CommonDialog1.FilterIndex = 2 ' 指定缺省的过滤器
CommonDialog1.ShowOpen ' 显示“打开”对话框
' 显示选定文件的名字
'MsgBox CommonDialog1.FileName
Open CommonDialog1.FileName For Binary As #1
ReDim aryContent(LOF(1) - 1)
Get #1, , aryContent
Close #1
strWj = StrConv(aryContent, vbUnicode)
RichTextBox1 = strWj
Exit Sub
ErrHandler:
' 用户按了“取消”按钮
Exit Sub
End Sub
exe文件不能存放在string类型的变量内,而就存放在byte类型的数组内。下面代码读取速度很快,你可试试:
Dim handle As Integer,bb() as Byte
handle = FreeFile
Open FileName$ For Binary As #handle
get #handle,,bb
Close #handle
'此时exe文件的内容已经存放到数组bb内了。
楼上所言甚是,二进制流远比字符读取要来得快。
二进制流远比字符读取要来得快。
Private Function GetFileText(FileName As String) As String
Dim tmpString As String
Dim Data() As Byte
Dim tmpFileNum As Integer
tmpFileNum = FreeFile()
Open FileName For Binary As #tmpFileNum
ReDim Data(LOF(tmpFileNum) - 1)
Get #tmpFileNum, , Data()
Close #tmpFileNum
tmpString = StrConv(Data, vbUnicode)
GetFileText = tmpString
End Function