如何读写一个文本文件的指定行?

解决方案 »

  1.   

    '引用microsoft script runtime
    Private Function ReadFileLine(cfile As String, cLine As Integer) As String
        Dim I As Integer
        Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream
        Set file1 = fsoTest.GetFile(cfile)
        Set ts = file1.OpenAsTextStream(ForReading)
        I = 1
        Do While Not ts.AtEndOfStream
            If I <> cLine Then
                ts.ReadLine
            Else
                ReadFileLine = ts.ReadLine
            End If
            I = I + 1
        Loop
        MsgBox "总行数" & I
        Set ts = NothingEnd FunctionPrivate Sub Form_Load()
       MsgBox "读取行的内容为:" & (ReadFileLine("f:\test.txt", 3))End Sub