VB file read/write Note 2

Source: Internet
Author: User

1. Read the TXT text file to the text box

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 1End SubPrivate 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 1End SubPrivate Sub Form_Load()'End Sub

Private Type Record    ID As Integer    Name As String * 30End TypePrivate 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 1End SubPrivate Sub Command2_Click()'    Call tofile1("Command2_Click")    Open App.Path & "\a2.txt" For Output As #1        Print #1, "abc" & "|" & "bbbb", 123456, "abcefge"    Close #1End SubPrivate Sub Command3_Click()'    Call tofile1("Command3_Click")    Open App.Path & "\a3.txt" For Output As #1        Write #1, 123456, "abcdefg"    Close #1End SubPrivate Sub Command4_Click()'    Call tofile1("Command4_Click")End SubPrivate Sub tofile1(strTmp As String)'    Open App.Path & "\a4.txt" For Append As #1        Write #1, strTmp    Close #1End SubPrivate 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 #1End SubPrivate 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 #1End SubPrivate 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, i, char            DoEvents        Next i    CloseEnd SubPrivate Sub Command8_Click()'    End Sub

The FileSystemObject object is not a built-in VB object. before using it, you must select [project] → [Reference] and select "Microsoft scripting RunTime" in the displayed window ", then, the fileexists method of FileSystemObject is used to determine whether the object exists. The sample code is as follows:

'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 SubPrivate 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.ShortNameEnd SubPrivate 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.CloseEnd SubPrivate 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.CloseEnd SubPrivate 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.CloseEnd SubPrivate Sub Command6_Click()'    Dim MyFSO As New FileSystemObject    Dim MyFile As File    Dim MyTS As TextStream        Set MyTS = MyFSO.OpenTextFile(App.Path & "\a8.txt", ForAppending, True)'    MyTS.Write ("abcdefg")    MyTS.Write (Me.Text1.Text)    MyTS.CloseEnd 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 SubPrivate Sub Command2_Click()'    Dim lenReturn As Long    lenReturn = WritePrivateProfileString("abc", "a2", "aaaabbcdefg", App.Path & "\aaa.ini")    Debug.Print lenReturnEnd Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.