StreamReader、StreamWriter - 我需要什么参考库?如何让下面的代码工作?
StreamReader, StreamWriter - what reference library do I need? How do I get the code below to work?
Sub UseStreamReader ()
Dim fileReader As System.IO.StreamReader
fileReader =
My.Computer.FileSystem.OpenTextFileReader("C:\testfile.txt")
Dim stringReader As String
stringReader = fileReader.ReadLine()
MsgBox("The first line of the file is " & stringReader)`enter code here`
End Sub
这更容易:
Sub UseStreamReader ()
For Each line in File.ReadLines("C:\testfile.txt")
MsgBox("The line of the file is " & line )
Next line
End Sub
ReadLines 增量读取;如果您在读取整个文件之前停止,则它不会将文件读入内存。这与读取所有内容的 File.ReadAllLines
形成对比,而 returns 你是一个数组
这些都在 System.IO
命名空间中(包括您的代码)。您需要引用系统(每个项目都应该有这个)和 class 顶部的一行,上面写着
Imports System.IO
如果您没有进口声明,则每次使用都必须完全合格:
For Each line in System.IO.File.ReadLines...
Sub UseStreamReader ()
Dim fileReader As System.IO.StreamReader
fileReader =
My.Computer.FileSystem.OpenTextFileReader("C:\testfile.txt")
Dim stringReader As String
stringReader = fileReader.ReadLine()
MsgBox("The first line of the file is " & stringReader)`enter code here`
End Sub
这更容易:
Sub UseStreamReader ()
For Each line in File.ReadLines("C:\testfile.txt")
MsgBox("The line of the file is " & line )
Next line
End Sub
ReadLines 增量读取;如果您在读取整个文件之前停止,则它不会将文件读入内存。这与读取所有内容的 File.ReadAllLines
形成对比,而 returns 你是一个数组
这些都在 System.IO
命名空间中(包括您的代码)。您需要引用系统(每个项目都应该有这个)和 class 顶部的一行,上面写着
Imports System.IO
如果您没有进口声明,则每次使用都必须完全合格:
For Each line in System.IO.File.ReadLines...