加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

VB 文件读写笔记2

发布时间:2020-12-16 22:40:46 所属栏目:大数据 来源:网络整理
导读:1.读取txt文本文件到文本框 Private Sub Command1_Click()' Dim str As String Open "d:My DocumentsVB6T12010901a1.txt" For Input As 1 Do While Not EOF(1) Input #1,str Me.Text1.Text = Me.Text1.Text vbNewLine str Loop Close 1End SubPrivate Su


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


Private Sub Command1_Click()
'
    Dim str As String
    Open "d:My DocumentsVB6T12010901a1.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 DocumentsVB6T12010901a1.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 SettingsjingMy Documentsvbt12011001a1.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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读