【问题标题】:How Do I filter out names of folders in C#?如何在 C# 中过滤掉文件夹的名称?
【发布时间】:2012-05-17 05:52:00
【问题描述】:

我让代码在目录中搜索并挑选出所有文件夹,但我只希望它挑选出以数据开头的文件夹。我该怎么做?

下面是我通过目录的代码:

    string[] filePaths = Directory.GetDirectories(defaultPath).Where(Data => !Data.EndsWith(".")).ToArray();

【问题讨论】:

    标签: c# .net string filter directory


    【解决方案1】:

    无需使用 LINQ; GetDirectories 支持搜索模式,并且可能会明显更快,因为过滤可能由文件系统完成,在 .NET 中枚举结果之前。

    string[] filePaths = Directory.GetDirectories(defaultPath, "Data*");
    

    请注意,* 是一个匹配零个或多个字符的通配符。

    【讨论】:

      【解决方案2】:

      如果“以数据开头”,您只是表示文件夹名称以“数据”开头,这将起作用

      string[] filePaths = Directory.GetDirectories(defaultPath)
          .Where(s => s.StartsWith("Data") && !s.EndsWith(".")).ToArray();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-12
        • 2015-06-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多