vb用fso读写文本文件

'需要引用 Microsoft Scripting Runtime
Dim fso As New FileSystemObject
Dim oText As TextStream
Dim fileName$


Private Sub Form_Load()
    Dim s$

    fileName = App.Path & "\save.txt"    '得到选择的文件
    If Not fso.FileExists(fileName) Then
        fso.CreateTextFile fileName
    End If
    Set oText = fso.OpenTextFile(fileName, ForReading, False, TristateUseDefault)

    While Not oText.AtEndOfLine
        s = oText.ReadLine
        List1.AddItem s, 0
    Wend
    oText.Close
    List1.AddItem "运行时间: " & Now, 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Set oText = fso.OpenTextFile(fileName, ForWriting, False, TristateUseDefault)
  Dim i%
  For i = List1.ListCount - 1 To 0 Step -1
    oText.WriteLine List1.List(i)
  Next
  oText.Close
End Sub
posted @ 2013-05-06 17:40  忘忧般若汤  阅读(2403)  评论(0编辑  收藏  举报