仅获取名称不以特定字符串开头的目录中的那些文件

Get only those files in a directory whose name does not start with a certain string

我目前有这个代码:

    Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo("C:\Scratch")

    For Each FInfo As IO.FileInfo In FolderInfo.GetFiles("*.xlsx")

        MessageBox.Show(FInfo.ToString())
    Next FInfo

显然,这将获取所有匹配模式“*.xlsx”的文件 - 但我对以 ​​"old" 开头的任何文件不感兴趣 - 所以当然在 For Next 中,我可以做类似 if If Not FInfo.Name.StartsWith("old") Then ... 然后做我需要做的事情,但我想知道是否有任何方法可以告诉 GetFiles 只获取“不”的文件t 以 "old" 开头并以 *.xlsx" 结尾?

我在 C# 中看到过我认为使用 LINQ 的示例 - 所以在 GetFiles 之后有类似“.Where(f => !(f.FullName.StartsWith("old")))”的内容但不确定 VB.NET 的等价物是什么(如果有的话)?

干杯,

克里斯

语法有点冗长,但 Where 在 VB

中也适用
For Each FInfo As IO.FileInfo In FolderInfo.GetFiles("*.xlsx").
    Where(Function(x) Not x.Name.StartsWith("old"))

    MessageBox.Show(FInfo.ToString())
Next FInfo

我还要添加一个 StringComparison.CurrentCultureIgnoreCase 以删除以 "Old" 或 "OLD" 开头的文件,依此类推

For Each FInfo As IO.FileInfo In FolderInfo.GetFiles("*.xlsx").
    Where(Function(x) Not x.Name.StartsWith("old", StringComparisong.CurrentCultureIgnoreCase))

    MessageBox.Show(FInfo.ToString())
Next FInfo

顺便说一句,你应该使用 属性 Name 而不是 FullName。 FullName returns 也是文件的路径,显然,此路径不以 "old".

开头
Dim folder As String = "C:\Scratch"
Dim files = Directory.EnumerateFiles(folder, "*.xlsx", SearchOption.TopDirectoryOnly) _
     .Where(Function(f) Not Path.GetFileName(f).ToLowerInvariant().StartsWith("old"))     

  For Each file As string In files
        MessageBox.Show(file)
  Next file