开发者

How can I read data from a text file using VB6?

I need to read data from text files and use the same in my application. Im using V开发者_StackOverflow社区B 6.0. What commands do I use? Some Sample code would be highly appreciated.


Here's how to read an entire text file into a string - from the VB6 manual.

Function FileToString(strFilename As String) As String
  iFile = FreeFile
  Open strFilename For Input As #iFile
    FileToString = StrConv(InputB(LOF(iFile), iFile), vbUnicode)
  Close #iFile
End Function


A full tutorial and sample code can be found here

  Open Filename$ For Input As #FileHandle

  Do While Not EOF(FileHandle)        ' Loop until end of file
   Line Input #FileHandle, TextLine$  ' Read line into variable
    ' Your code here
  Loop

  Close #FileHandle


I'm a little late to the game here, but the FileSystemObject that is part of the Microsoft Scripting Runtime (scrrun.dll) can be pretty useful for this.

Public Function ReadTextFileAsString(IN_sFilePath As String) As String
    Dim myFSO As Scripting.FileSystemObject
    Dim myTextStream As Scripting.TextStream
    Dim myString As String

    'Create a new FileSystemObject
    Set myFSO = New Scripting.FileSystemObject

    'Make sure file exists:
    If myFSO.FileExists(IN_sFilePath) Then
        Set myTextStream = myFSO.OpenTextFile(IN_sFilePath, ForReading)
        myString = myTextStream.ReadAll()
        Call myTextStream.Close
    End If
    'Assign Return Value
    ReadTextFileAsString = myString

    'Make sure to clean up when done.
    Set myTextStream = Nothing
    Set myFSO = Nothing
End Function

There are a number of other methods available for getting data from the text stream. You can also read a certain number of characters at a time, or line-by-line. You will need to add the Microsoft Scripting Runtime into your project references, but it is really very useful.


Make sure your file exists:

If myFSO.FileExists(IN_sFilePath) Then
    Set myTextStream = myFSO.OpenTextFile(IN_sFilePath, ForReading)
    myString = myTextStream.ReadAll()
    Call myTextStream.Close
End If
'Assign Return Value
ReadTextFileAsString = myString


if there is just plain text in the file then you can read in the whole into 1 string variable with the following code :

Private Sub ReadFile(strFile As String)
  Dim intFile As Integer
  Dim strData As String
  intFile = FreeFile
  Open strFile For Input As #intFile
    strData = Input(LOF(intFile), #intFile)
  Close #intFile
End Sub

a variable-length string can contain up to approximately 2 billion (2^31) characters


Here is the code for that

Function ReadFileToText(filePath)

   Dim objFile, objText, text

   Set objFile = CreateObject("Scripting.FileSystemObject")
   Set objText = objFile.OpenTextFile(filePath)

   text = objText.ReadAll
   objText.Close

   Set objText = Nothing
   Set objFile = Nothing

   ReadFileToText = text

End Function


i will refer you a different method to read and import the content to your form window

public sub readfile
    Dim rtc As TextBox = New TextBox
    rtc.Multiline = True
    rtc.ScrollBars = ScrollBars.Both
    rtc.Width = 400
    rtc.Height = 200
    Me.Controls.Add(rtc)
    rtc.WordWrap = True
    Dim FILE_NAME As String = "C:\Users\vcidex92\Desktop\suji\me.html"

    If System.IO.File.Exists(FILE_NAME) = True Then

        Dim objReader As New System.IO.StreamReader(FILE_NAME)
        rtc.Text = objReader.ReadToEnd
        objReader.Close()
    Else

        MsgBox("File Does Not Exist")
    End If
end sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜