2011-11-21 48 views

回答

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 
+0

不能在VB6中工作...顯然這是.NET或更好 –

+2

@SetSailMedia不正確,這是VB6。它需要引用'scrrun.dll'('Microsoft Scripting Runtime') – Brad

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

不適用於所有區域設置。只需使用VB6手冊中的代碼,就像我在[此重複問題]的答案中一樣(http://stackoverflow.com/questions/2873830/how-can-i-read-data-from-a-text-file-using -vb6/2875248#2875248) – MarkJ

-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 
+0

不必要的逐行循環。不需要關閉TextStream。 – CJ7

+1

首先不需要使用FSO。你的答案也一樣。 – wqw

+0

@wqw:使用它沒有任何傷害。 – CJ7

相關問題