VB 快速读取文件内容的方法

来源:互联网 发布:cs成像算法 编辑:程序博客网 时间:2024/06/05 02:10
读取text文件的最快方法是使用Input$函数,就象下面的过程:
Function FileText(ByVal filename As String) As String
Dim handle As Integer' 判断文件存在性
If Len(Dir$(filename)) = 0 Then Err.Raise 53 '文件没有找到
End If
' 以binary模式打开文件
handle = FreeFile
Open filename$ For Binary As #handle
' 读取内容,关闭文件
FileText = Space$(LOF(handle))Get #handle, , FileText
Close #handle
End Function
使用上述方法要比使用Input命令读取文件每一行的方法快很多。下面是应用这个函数读取Autoexec.bat的内容到多行textbox控件的例子:
Text1.Text = FileText("c:\autoexec.bat")

无闪烁地快速附加字符串到TextBox控件
附加文本到TextBox或者RichTextBox控件的通常方法是在当前内容上连接上新的字符串:
Text1.Text = Text1.Text & newString
但还有一个更快的方法,并且会减少连接操作的闪烁感,代码如下:
Text1.SelStart = Len(Text1.Text)
Text1.SelText = newString

原创粉丝点击