您的位置:逆风者 VB 正文
原作者:www.upwinder.com 添加时间:2007-09-12 原文发表:2007-08-31 人气:199 来源:未知

读取text文件的最快方法是使用Input$函数,就象下面的过程:

Function FileText (filename$) As String

逆风者

Dim handle As Integer

handle = FreeFile

Open filename$ For Input As #handle

FileText = Input$(LOF(handle), handle)

Close #handle

End Function

使用上述方法要比使用Input命令读取文件每一行的方法快很多。下面是应用这个函数读取Autoexec.bat的内容到多行textbox控件的例子:

Text1.Text = FileText("c:\autoexec.bat")

但请注意:当文件包含Ctrl-Z(EOF)字符时,上面的函数代码可能会发生错误。因此,要修改一下代码:

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

相关文章

VB“超频”秘籍之给字符串提速
提高Visual Basic访问数据库的效率
在数据库中不用 EOF 以加快记录循环
提高Visual Basic访问数据库效率
如何编写高质量的VB代码
Friend过程快于Public过程
字体对象克隆招法
如何去优化你的VB程序
VB“变态”用法之高效字串指针类
Visual Basic代码优化的六条军规
快速调入TreeView控件以及ListView控件的子
Visual Basic 高级编程及其项目应用开发

相关评论


本文章所属分类:首页 VB

  热门关键字: