微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

VB 文件读写笔记2


1.读取txt文本文件到文本框


Private Sub Command1_Click()
'
    Dim str As String
    Open "d:\My Documents\VB6\T12010901\a1.txt" For Input As 1
        do while Not EOF(1)
            Input #1,str
            Me.Text1.Text = Me.Text1.Text & vbNewLine & str
        Loop
    Close 1
End Sub

Private Sub Command2_Click()
'
    Dim MyLine
    Open "d:\My Documents\VB6\T12010901\a1.txt" For Input As 1
        do while Not EOF(1)
            Line Input #1,MyLine
            Me.Text1.Text = Me.Text1.Text + MyLine + vbCrLf
        Loop
    Close 1
End Sub

Private Sub Form_Load()
'

End Sub


Private Type Record
    ID As Integer
    Name As String * 30
End Type

Private Sub Command1_Click()
'
    Call tofile1("Command1_Click")
    Open "C:\Documents and Settings\jing\My Documents\vb\t12011001\a1.txt" For Binary As 1
        Me.Text1.Text = Input(LOF(1),1)
    Close 1
End Sub

Private Sub Command2_Click()
'
    Call tofile1("Command2_Click")
    Open App.Path & "\a2.txt" For Output As #1
        Print #1,"abc" & "|" & "bbbb",123456,"abcefge"
    Close #1
End Sub

Private Sub Command3_Click()
'
    Call tofile1("Command3_Click")
    Open App.Path & "\a3.txt" For Output As #1
        Write #1,"abcdefg"
    Close #1
End Sub

Private Sub Command4_Click()
'
    Call tofile1("Command4_Click")
End Sub

Private Sub tofile1(strTmp As String)
'
    Open App.Path & "\a4.txt" For Append As #1
        Write #1,strTmp
    Close #1
End Sub

Private Sub Command5_Click()
'
    Dim MyRecord As Record,Position
    Open App.Path & "\a5.txt" For Random As #1 Len = Len(MyRecord)
        Position = 4
        Get #1,Position,MyRecord
        MsgBox MyRecord.Name
    Close #1
End Sub


Private Sub Command6_Click()
'
    Dim MyRecord As Record,recordnumber
    Open App.Path & "\a5.txt" For Random As #1 Len = Len(MyRecord)
        For recordnumber = 1 To 5
            MyRecord.ID = recordnumber
            MyRecord.Name = "aaaa" & recordnumber
            Put #1,recordnumber,MyRecord
        Next recordnumber
    Close #1
End Sub

Private Sub Command7_Click()
'
    Dim char() As Byte
    ReDim char(1 To 1024)
    Open App.Path & "\a5.txt" For Binary As #1
    Open App.Path & "\a6.txt" For Binary As #2
        For i = 1 To FileLen(App.Path & "\a5.txt") Step 1024
            If FileLen(App.Path & "\a5.txt") - i + 1 < 1024 Then
                ReDim char(1 To FileLen(App.Path & "\a5.txt") - i + 1)
            End If
            Get #1,i,char
            Put #2,char
            DoEvents
        Next i
    Close
End Sub

Private Sub Command8_Click()
'
    
End Sub

filesystemobject对象不是vb内置对象,使用前必须首先选择[工程]→[引用],在出现的窗口中选择“microsoft scripting runtime”,然后利用filesystemobject的fileexists方法来判断文件是否存在。示例程序代码如下:
'Dim MyFSO As New FileSystemObject
'Dim MyDrive As Drive
'Set MyDrive = MyFSO.GetDrive("c:")
Private Sub Command1_Click()
'
    Dim MyFSO As New FileSystemObject
    Dim MyDrive As Drive
    For Each MyDrive In MyFSO.Drives
        Print MyDrive.DriveLetter
'        Print MyDrive.AvailableSpace
    Next
    Set MyDrive = MyFSO.GetDrive("c:")
    Print MyDrive.AvailableSpace
    Print MyDrive.DriveType
    Print MyDrive.FileSystem
    Print MyDrive.FreeSpace
    
    Print MyDrive.SerialNumber
    Print MyDrive.Path
    Print MyDrive.SerialNumber
    Print MyDrive.TotalSize
    Print MyDrive.VolumeName
    
End Sub


Private Sub Command2_Click()
'
    Dim MyFSO As New FileSystemObject
    Dim MyFolder As Folder
    Dim sFolder As Folder
    Set MyFolder = MyFSO.GetFolder("c:\")
    Print MyFolder.Path & "have: "
    For Each sFolder In MyFolder.SubFolders
        Debug.Print sFolder.Name
    Next

'    Debug.Print sFolder.Type
'    Debug.Print sFolder.DateCreated
'    Debug.Print sFolder.DateLastModified
'    Debug.Print sFolder.Drive
'    Debug.Print sFolder.Attributes
'    Debug.Print sFolder.Size
'    Debug.Print sFolder.Path
'    Debug.Print sFolder.ShortName
End Sub

Private Sub Command3_Click()
'
    Dim MyFSO As New FileSystemObject
    Dim MyTS As TextStream
    Set MyTS = MyFSO.OpenTextFile(App.Path & "\a6.txt")
    Set MyTS = MyFSO.OpenTextFile(App.Path & "\a7.txt",ForAppending,True)
    Print "ok"
    MyTS.Close
End Sub

Private Sub Command4_Click()
'
    Dim MyFSO As New FileSystemObject
    Dim MyFile As File
    Dim MyTS As TextStream
    Set MyFile = MyFSO.GetFile(App.Path & "\a7.txt")
    Set MyTS = MyFile.OpenAsTextStream(ForAppending)
    Print "ok"
    MyTS.Close
End Sub

Private Sub Command5_Click()
'
    Dim MyFSO As New FileSystemObject
    Dim MyFile As File
    Dim MyTS As TextStream
    Dim MyStr,strTmp1 As String
    Dim FileName As String
    Me.Text1.Text = ""
    strTmp1 = App.Path & "\a4.txt"
    Debug.Print strTmp1
    Set MyTS = MyFSO.OpenTextFile(App.Path & "\a4.txt",ForReading,False)
    Set MyFile = MyFSO.GetFile(App.Path & "\a4.txt")
    do while Not MyTS.AtEndOfStream
        MyStr = MyTS.ReadLine
        Me.Text1.Text = Me.Text1.Text + MyStr + vbCrLf
        Me.Text1.Refresh
    Loop
    MyTS.Close
End Sub

Private Sub Command6_Click()
'
    Dim MyFSO As New FileSystemObject
    Dim MyFile As File
    Dim MyTS As TextStream
    
    Set MyTS = MyFSO.OpenTextFile(App.Path & "\a8.txt",True)
'    MyTS.Write ("abcdefg")
    MyTS.Write (Me.Text1.Text)
    MyTS.Close
End Sub


Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
                        ByVal lpApplicationName As String,_
                        ByVal lpKeyName As Any,ByVal lpDefault As String,_
                        ByVal lpReturnedString As String,ByVal nSize As Long,_
                        ByVal lpFileName As String) As Long
                        
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
                        ByVal lpApplicationName As String,ByVal lpKeyName As Any,ByVal lpString As Any,_
                        ByVal lpFileName As String) As Long
                        
Private Sub Command1_Click()
'
    Dim strReturn As String,lenReturn As Long
    strReturn = vbNullString
    Debug.Print strReturn
    strReturn = Space(&HFE)
    Debug.Print strReturn
    lenReturn = GetPrivateProfileString("abc","a1",vbNullString,strReturn,&HFF,App.Path & "\aaa.ini")
    Debug.Print strReturn
    Debug.Print lenReturn
    Me.Caption = Trim(Replace(Left(strReturn,lenReturn),Chr(0) & Chr(0),Chr(0)))
End Sub

Private Sub Command2_Click()
'
    Dim lenReturn As Long
    lenReturn = WritePrivateProfileString("abc","a2","aaaabbcdefg",App.Path & "\aaa.ini")
    Debug.Print lenReturn
End Sub

原文地址:https://www.jb51.cc/vb/260487.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐