代码之家  ›  专栏  ›  技术社区  ›  Nimisha Prajapati

具有多个扩展名筛选器和按文件名排序的GetFile

  •  1
  • Nimisha Prajapati  · 技术社区  · 6 年前

    我正在开发vb.net桌面应用程序。现在,我需要来自目录的文件具有扩展名.txt和.sql,还需要按文件夹名排序的文件。两个都需要怎么做?

      Try
                    Dim s As String = Txtfolder.Text
    
    
                    Dim files As List(Of String) = New List(Of String)()
                    Try
                      For Each f As String In Directory.GetFiles(s, "*.*").Where(Function(f1) f1.EndsWith(".sql") OrElse f1.EndsWith(".txt")).OrderBy(Function(f) f.LastWriteTime).First()
                            files.Add(f)
                        Next
    
                        For Each d As String In Directory.GetDirectories(s)
                            files.AddRange(DirSearch(d))
                        Next
                    Catch excpt As System.Exception
                        MessageBox.Show(excpt.Message)
                    End Try
    
    
      Private Function DirSearch(ByVal sDir As String) As List(Of String)
            Dim files As List(Of String) = New List(Of String)()
            Try
                For Each f As String In Directory.GetFiles(sDir, "*.*").Where(Function(f1) f1.EndsWith(".sql") OrElse f1.EndsWith(".txt"))
                    files.Add(f)
                Next
    
                For Each d As String In Directory.GetDirectories(sDir)
                    files.AddRange(DirSearch(d))
                Next
            Catch excpt As System.Exception
                MessageBox.Show(excpt.Message)
            End Try
    
            Return files
        End Function
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   jmcilhinney    6 年前

    下面是我的注释中选项1的示例,即获取所有文件路径并自己筛选:

    Dim folderPath = "folder path here"
    Dim filePaths = Directory.GetFiles(folderPath).
                              Where(Function(s) {".txt", ".sql"}.Contains(Path.GetExtension(s))).
                              OrderBy(Function(s) Path.GetFileName(s)).
                              ToArray()
    

    下面是选项2的一个示例,即通过扩展和组合获取路径:

    Dim folderPath = "folder path here"
    Dim filePaths = Directory.GetFiles(folderPath, "*.txt").
                              Concat(Directory.GetFiles(folderPath, "*.sql")).
                              OrderBy(Function(s) Path.GetFileName(s)).
                              ToArray()
    
        2
  •  0
  •   Jimi    6 年前

    另一种方法,允许搜索多个目录并使用多个搜索模式筛选结果。
    它返回一个命令 List(Of String) :

    Private Function DirSearch(ByVal sDirList As String(), SearchPatter As String()) As List(Of String)
        Return sDirList.SelectMany(
            Function(dir) SearchPatter.SelectMany(
                Function(filter)
                    Return Directory.GetFiles(dir, filter, SearchOption.AllDirectories)
                End Function).OrderBy(Function(xDir) xDir)).ToList()
    End Function
    

    可以将路径列表和扩展列表传递给该方法:

    Dim SearchPaths As String() = New String() {"[Directory1]", "[Directory2]"}
    Dim ItemSearchPattern As String() = New String() {"*.txt", "*.sql", "*.jpg"}
    
    Dim DirListing As List(Of String) = DirSearch(SearchPaths, ItemSearchPattern)
    

    提取sigle目录的内容时使用:

    Dim FilesInDir As List(Of String) = DirListing.
                      Where(Function(entry) entry.ToUpper().
                      Contains("[DirectoryName]".ToUpper())).ToList()
    

    这是一个不区分大小写的筛选器。删除( ToUpper() )对于区分大小写的。