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

String 如何使用VB6将文本文件加载到字符串中,string,vb6,text-processing,filesystemobject,String,Vb6,Text Processing,Filesystemobject,如何使用VB6将文本文件快速加载到字符串中?这是在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

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

这是在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

以下是使用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

不必要的逐行循环。不需要关闭TextStream。首先不需要使用FSO。你的答案也是一样。@CraigJ:没有坏处,除非某个伪造的卸载程序注销COM服务器。@wqw:然后你在应用程序中使用self-heal MSI安装程序-问题已解决。的可能副本不会在所有区域设置上都起作用。只要使用VB6手册中的代码,就像我回答不在VB6中工作一样。。。很明显,这是.NET或better@SetSailMedia不正确,这是VB6。它需要引用
scrrun.dll
Microsoft脚本运行时
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