如何使用VB6将文本文件加载到字符串中

时间:2011-11-21 07:24:45

标签: string vb6 text-processing filesystemobject

如何使用VB6快速将文本文件加载到字符串中?

3 个答案:

答案 0 :(得分:8)

这是在VB6中逐行加载整个文件的最快方法:

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

答案 1 :(得分:3)

Public Function ReadFileIntoString(strFilePath As String) As String

    Dim fso As New FileSystemObject
    Dim ts As TextStream

    Set ts = fso.OpenTextFile(strFilePath)
    ReadFileIntoString = ts.ReadAll

End Function 

答案 2 :(得分:-2)

以下是使用filesystemobject执行此操作的一种方法:

Public Function ReadTextFileIntoString(strPathToFile as String) as String
  Dim objFSO As New FileSystemObject
  Dim objTxtStream As TextStream        
  Dim strOutput as String
  Set objTxtStream = objFSO.OpenTextFile(strPathToFile)
  Do until objTxtStream.AtEndOfStream
   strOutput = strOutput + objTxtStream.ReadLine
  Loop

  objTxtStream.Close
  ReadTextFileIntoString = strOutput
End Sub