【问题标题】:Search specific extensions in a directory VB在目录 VB 中搜索特定扩展
【发布时间】:2016-01-21 12:23:52
【问题描述】:

我正在尝试在“C:\test”和子目录中查找所有 .txt 文件。

目前我已经设法让它在我的文档顶层搜索 .txt 文件。

Public Class Form1
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'Dim path As String = "C:\\"
    Dim txt As String = "*.txt"

    For Each foundFile As String In My.Computer.FileSystem.GetFiles(
      My.Computer.FileSystem.SpecialDirectories.MyDocuments,
      FileIO.SearchOption.SearchAllSubDirectories, txt)

      ListBox1.Items.Add(foundFile)
    Next


  End Sub
End Class

我想要类似的东西:

My.Computer.FileSystem.path,
FileIO.SearchOption.SearchAllSubDirectories, txt)

由于某些未知原因,搜索所有子目录不起作用。

【问题讨论】:

  • 你有什么异常吗?我用 path="C:\" 尝试了你的代码并且它工作但我在某些 Windows 系统目录中得到了一个 UnauthorizedAccessException
  • 你能出示你的代码吗?
  • 所以我尝试使用具有path="C:\Test" 的代码。我在其中创建了一些子目录和一些文本文件。按预期工作。
  • Sub Main() Dim path As String = "C:\Test" Dim txt As String = "*.txt" For Each foundFile As String In My.Computer.FileSystem.GetFiles( path, FileIO.SearchOption.SearchAllSubDirectories, txt) System.Console.WriteLine(foundFile) Next End Sub
  • 需要以管理员身份运行才能搜索c:

标签: .net vb.net file


【解决方案1】:

Directory class(来自 System.IO 命名空间)具有所需的方法,您不需要 For Each 循环

Dim path As String = "C:\test"
Dim txt As String = "*.txt"

ListBox1.Items.AddRange(Directory.EnumerateFiles(path, txt, SearchOption.AllDirectories ).ToArray())           

请记住,任何尝试读取保留的文件系统文件夹(如 C:\System Volume Information)都会导致 UnauthorizedAccessException,因此,如果您的路径变量是动态的,那么请准备好最终捕获此调用引发的任何异常

编辑

这是Mr Gravellthis question 中显示的过程在VB.NET 中的转换,他解释了一种从系统驱动器的根目录遍历所有目录的方法,而不会因某些系统文件夹引发的异常而停止(如System Volume InformationProgram

Delegate Sub ProcessFileDelegate(ByVal path As String)

Sub Main
    Dim path = "c:\"
    Dim ext = "*.txt"
    Dim runProcess As ProcessFileDelegate = AddressOf ProcessFile
    ApplyAllFiles(path, ext, runProcess)
End Sub

Sub ProcessFile(ByVal path As String)
    ' This is the sub where you process the filename passed in'
    ' you add it to your listbox or to some kind of collection '
    ListBox1.Items.Add(path)
End Sub

' This is the recursive procedure that traverse all the directory and'
' pass each filename to the delegate that adds the file to the listbox'
Sub ApplyAllFiles(ByVal folder As String, ByVal extension As String, ByVal fileAction As ProcessFileDelegate)
    For Each file In Directory.GetFiles(folder, extension)
        fileAction.Invoke(file)
    Next    
    For Each subDir In Directory.GetDirectories(folder)
        Try
            ApplyAllFiles(subDir, extension, fileAction)
        Catch ex As Exception
            ' Console.WriteLine(ex.Message)'
            ' Or simply do nothing '
        End Try
    Next

End Sub

【讨论】:

  • 在返回错误时是否需要声明这个,Directory is not declared & SearchOption
  • 是的,你需要 Imports System.IO
  • 谢谢史蒂夫,这很好用,但我不能从 C:\ 执行它(它可能不是我想要搜索的 .txt 文件。)
  • 正如我在答案中所说,某些路径会引发 AccessUnauthorized 异常,因为出于安全考虑,SO 会阻止对这些文件夹的任何访问。如果你设置 AllDirectories 并从 C:root 开始你不可避免地会匹配这个问题
  • 有没有办法只搜索未隐藏的文件夹?即用户可以看到的那些。编辑或者,如果发生错误,请忽略该路径并搜索下一个?
【解决方案2】:

你可以试试

Directory.GetFiles("searchpath", "extension")

而不是

My.Computer.FileSystem.GetFiles()

【讨论】:

  • 我不想从我的文档中获取它,我想从 C:\ 理想地获取它
猜你喜欢
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
  • 2013-04-10
  • 1970-01-01
相关资源
最近更新 更多