【问题标题】:File search pattern: to search all files that does not have a 0 in 11th position of file name in System.IO.Directory.GetFiles文件搜索模式:搜索 System.IO.Directory.GetFiles 中文件名第 11 位不为 0 的所有文件
【发布时间】:2015-12-10 20:35:14
【问题描述】:

在特定位置搜索文件名中没有 0 的所有文件的语法是什么 就我而言,我想搜索文件名第 11 位不包含 0 的所有文件 代替“*”如下图

string[] files = System.IO.Directory.GetFiles(sourcePath, fileName + "_*"); 

【问题讨论】:

  • 看看这个其他问答:stackoverflow.com/questions/13022988/…也许使用类似 SQL 语法的 WMI 可以完成这项工作
  • “第 11 位没有 0”与fileName + "_*" 有什么关系?你是说filename 总是九个字符吗?一些示例文件名会有所帮助。

标签: c# asp.net .net system.io.file


【解决方案1】:

不能做“不”,但下一个最好的办法是使用 LINQ 进行过滤。

string[] files = System.IO.Directory.GetFiles(sourcePath, fileName + "_*")
  .Where(x => !Path.GetFileName(x).StartsWith(fileName + "_0"))
  .ToArray();

【讨论】:

    【解决方案2】:

    或者你可以尝试如下表达式:

            string path = @"C:\";
            string parameter = "SomeParameter";
            string[] files = Array.FindAll(Directory.GetFiles(path), x => !Path.GetFileName(x).StartsWith(parameter));
    
            foreach (string file in files)
            {
                Console.WriteLine(file);
            }
    

    【讨论】:

      猜你喜欢
      • 2013-08-11
      • 2019-04-04
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 2014-07-24
      • 2012-05-21
      • 1970-01-01
      相关资源
      最近更新 更多