VB 文件读写笔记2

来源:互联网 发布:php switch 编辑:程序博客网 时间:2024/06/05 10:25


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 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

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 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


原创粉丝点击