读写文本文件到文本框-------------------------------------------------------------------------------- 把文本文件内容读取TextBox: Dim TempFile As Long Dim LoadBytes() As ByteTempFile=FreeFile Open 文件名 For Binary As #TempFile Redim LoadBytes(1 To Lof(TempFile)) As Byte Get #TempFile,,LoadBytes Close TempFileText1.Text=StrConv(LoadBytes,vbUniCode)把TextBox内容写入文本文件: Dim TempFile As Long Dim SaveBytes() As ByteSaveBytes=StrConv(Text1.Text,vbFromUniCode)TempFile=FreeFile Open 文件名 For Binary As #TempFile Put #TempFile,,SaveBytes Close TempFile
二、用FSO(FileSystemObject)工程”,“引用”,然后在“引用”对话框中选中“Microsoft Scripting Runtime”前的复选框,然后单击“确定”。在VB中新建一个工程,然后在Form1上添加两个命令按钮,然后输入以下代码:Private Sub CmdRead_Click() Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream, s As String Set file1 = fsoTest.GetFile(“C:\testfile.txt") Set ts = file1.OpenAsTextStream(ForReading) '读取一行 s = ts.ReadLine MsgBox s ts.Close End Sub Private Sub CmdWrite_Click() Dim txtfile As File, ts As TextStream Set txtfile = fsoTest.GetFile(“c:\testfile.txt") Set ts = txtfile.OpenAsTextStream(ForWriting) '使用Write方法写入一行。 ts.Write (“This is only a Test") ' 写入一行带有换行符的文本。 ts.WriteLine (“Testing 1, 2, 3.") ' 向文件中写入三个换行符。 ts.WriteBlankLines (3) ts.Close End Sub在这里我们假设已经在磁盘上建立了一个名为testfile.txt的文件。在按下F5运行时,先单击Write写入数据,然后按下Read读取数据。
dim strcontent as string open "c:\2003052100010830" for input as #1 input #1,strcontent close #1逐行读 dim strcontent as string,tmpstr as string open "c:\2003052100010830" for input as #1 do while not eof(1) line input #1,tmpstr strcontent=strcontent & tmpstr &vbcrlf loop close #1
'引用microsoft script runtime Private Function ReadFileLine(cfile As String, cLine As Integer) As String Dim I As Integer Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream Set file1 = fsoTest.GetFile(cfile) Set ts = file1.OpenAsTextStream(ForReading) I = 1 Do While Not ts.AtEndOfStream If I <> cLine Then ts.ReadLine Else ReadFileLine = ts.ReadLine End If I = I + 1 Loop MsgBox "总行数" & I Set ts = NothingEnd FunctionPrivate Sub Form_Load() MsgBox "读取行的内容为:" & (ReadFileLine("f:\test.txt", 3))End Sub
把文本文件内容读取TextBox:
Dim TempFile As Long
Dim LoadBytes() As ByteTempFile=FreeFile
Open 文件名 For Binary As #TempFile
Redim LoadBytes(1 To Lof(TempFile)) As Byte
Get #TempFile,,LoadBytes
Close TempFileText1.Text=StrConv(LoadBytes,vbUniCode)把TextBox内容写入文本文件:
Dim TempFile As Long
Dim SaveBytes() As ByteSaveBytes=StrConv(Text1.Text,vbFromUniCode)TempFile=FreeFile
Open 文件名 For Binary As #TempFile
Put #TempFile,,SaveBytes
Close TempFile
Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream, s As String
Set file1 = fsoTest.GetFile(“C:\testfile.txt")
Set ts = file1.OpenAsTextStream(ForReading)
'读取一行
s = ts.ReadLine
MsgBox s
ts.Close
End Sub
Private Sub CmdWrite_Click()
Dim txtfile As File, ts As TextStream
Set txtfile = fsoTest.GetFile(“c:\testfile.txt")
Set ts = txtfile.OpenAsTextStream(ForWriting)
'使用Write方法写入一行。
ts.Write (“This is only a Test")
' 写入一行带有换行符的文本。
ts.WriteLine (“Testing 1, 2, 3.")
' 向文件中写入三个换行符。
ts.WriteBlankLines (3)
ts.Close
End Sub在这里我们假设已经在磁盘上建立了一个名为testfile.txt的文件。在按下F5运行时,先单击Write写入数据,然后按下Read读取数据。
open "c:\2003052100010830" for input as #1
input #1,strcontent
close #1逐行读
dim strcontent as string,tmpstr as string
open "c:\2003052100010830" for input as #1
do while not eof(1)
line input #1,tmpstr
strcontent=strcontent & tmpstr &vbcrlf
loop
close #1
Private Function ReadFileLine(cfile As String, cLine As Integer) As String
Dim I As Integer
Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream
Set file1 = fsoTest.GetFile(cfile)
Set ts = file1.OpenAsTextStream(ForReading)
I = 1
Do While Not ts.AtEndOfStream
If I <> cLine Then
ts.ReadLine
Else
ReadFileLine = ts.ReadLine
End If
I = I + 1
Loop
MsgBox "总行数" & I
Set ts = NothingEnd FunctionPrivate Sub Form_Load()
MsgBox "读取行的内容为:" & (ReadFileLine("f:\test.txt", 3))End Sub