StreamReader 在读取文件时抛出 System.IO.FileInfo[] 异常
StreamReader throws System.IO.FileInfo[] exception when reading files
每次我尝试使用 StreamReader
读取文件时,都会抛出以下异常:
System.IO.FileInfo[] cannot be found.
这是我的代码:
Dim dinfo As New DirectoryInfo(TextBox1.Text)
Dim files As FileInfo() = dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
Dim sr = New StreamReader(TextBox1.Text & "/" & dinfo.GetFiles.ToString)
在我的代码中还有这个示例,我不知道它是否相关但以防万一:
Dim dinfo As New DirectoryInfo(TextBox1.Text)
Dim files As FileInfo() = dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
ListBox1.Items.Clear()
For Each file As FileInfo In files
ListBox1.Items.Add(file.Name)
Next
我正在尝试将找到的所有 .txt 文件传递给 StreamReader
,以便 StreamReader
可以一个接一个地读取所有找到的 .txt 文件。
如果我没记错的话,问题很简单是因为你将一个数组传递给 StreamReader
但 StreamReader
是一次读取一个文件。
我在这里介绍的是使用您的 ListBox 方法,但这不是推荐的版本。
子以将所有 *.txt 文件添加到列表框:
Public Sub AddToList(ByVal pather As String)
Dim Names As String() = Directory.GetFiles(pather, "*.txt", SearchOption.AllDirectories)
' Display all files.
For Each name As String In Names
ListBox1.Items.Add(name)
Next
End Sub
然后sub逐一阅读所有*.txt:
Public Sub ReadAllTxt()
For Each item In ListBox1.Items
Console.Write(File.ReadAllText(item))
Next
End Sub
更好的方法是在我们浏览文件时直接阅读:
Public Sub ReadAllTxt(ByVal pather As String)
Dim Names As String() = Directory.GetFiles(pather, "*.txt", SearchOption.AllDirectories)
' Display all files.
For Each name As String In Names
ListBox1.Items.Add(name)
Console.Write(File.ReadAllText(name))
Next
End Sub
你不能那样使用 dinfo.GetFiles.ToString
。这将 return System.IO.FileInfo[]
而 StreamReader 期待路径或流。我认为在这种情况下是一条路径。
此外,考虑使用 Path.Combine 而不是将字符串连接在一起构成路径。虽然最后我不认为你真的需要这个,但它值得一读。
我也会考虑在使用 StreamReader
时实现 Using:
Sometimes your code requires an unmanaged resource, such as a file handle, a COM wrapper, or a SQL connection. A Using block guarantees the disposal of one or more such resources when your code is finished with them. This makes them available for other code to use.
根据你的评论,我认为你想要的是这样的:
Dim dinfo As New DirectoryInfo(TextBox1.Text)
For Each f In dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
Using sr As New StreamReader(f.FullName)
While Not sr.EndOfStream
Debug.WriteLine(sr.ReadLine())
End While
End Using
Next
每次我尝试使用 StreamReader
读取文件时,都会抛出以下异常:
System.IO.FileInfo[] cannot be found.
这是我的代码:
Dim dinfo As New DirectoryInfo(TextBox1.Text)
Dim files As FileInfo() = dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
Dim sr = New StreamReader(TextBox1.Text & "/" & dinfo.GetFiles.ToString)
在我的代码中还有这个示例,我不知道它是否相关但以防万一:
Dim dinfo As New DirectoryInfo(TextBox1.Text)
Dim files As FileInfo() = dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
ListBox1.Items.Clear()
For Each file As FileInfo In files
ListBox1.Items.Add(file.Name)
Next
我正在尝试将找到的所有 .txt 文件传递给 StreamReader
,以便 StreamReader
可以一个接一个地读取所有找到的 .txt 文件。
如果我没记错的话,问题很简单是因为你将一个数组传递给 StreamReader
但 StreamReader
是一次读取一个文件。
我在这里介绍的是使用您的 ListBox 方法,但这不是推荐的版本。
子以将所有 *.txt 文件添加到列表框:
Public Sub AddToList(ByVal pather As String)
Dim Names As String() = Directory.GetFiles(pather, "*.txt", SearchOption.AllDirectories)
' Display all files.
For Each name As String In Names
ListBox1.Items.Add(name)
Next
End Sub
然后sub逐一阅读所有*.txt:
Public Sub ReadAllTxt()
For Each item In ListBox1.Items
Console.Write(File.ReadAllText(item))
Next
End Sub
更好的方法是在我们浏览文件时直接阅读:
Public Sub ReadAllTxt(ByVal pather As String)
Dim Names As String() = Directory.GetFiles(pather, "*.txt", SearchOption.AllDirectories)
' Display all files.
For Each name As String In Names
ListBox1.Items.Add(name)
Console.Write(File.ReadAllText(name))
Next
End Sub
你不能那样使用 dinfo.GetFiles.ToString
。这将 return System.IO.FileInfo[]
而 StreamReader 期待路径或流。我认为在这种情况下是一条路径。
此外,考虑使用 Path.Combine 而不是将字符串连接在一起构成路径。虽然最后我不认为你真的需要这个,但它值得一读。
我也会考虑在使用 StreamReader
时实现 Using:
Sometimes your code requires an unmanaged resource, such as a file handle, a COM wrapper, or a SQL connection. A Using block guarantees the disposal of one or more such resources when your code is finished with them. This makes them available for other code to use.
根据你的评论,我认为你想要的是这样的:
Dim dinfo As New DirectoryInfo(TextBox1.Text)
For Each f In dinfo.GetFiles("*.txt", SearchOption.AllDirectories)
Using sr As New StreamReader(f.FullName)
While Not sr.EndOfStream
Debug.WriteLine(sr.ReadLine())
End While
End Using
Next