【问题标题】:Query filesystem for list of files with certain attributes?查询文件系统以获取具有某些属性的文件列表?
【发布时间】:2011-03-23 03:13:23
【问题描述】:

我希望能够查询文件夹(和子文件夹)并获得满足特定属性特定条件的文件列表...例如,所有具有:

在 c:\somefolder 下
file_extension = ".abc"
x 和 y 之间的文件大小 KB
(文件名像 '%this' 或文件名像 '%that%' 和文件名不像 '%somethingelse%'
在 date1 和 date2 之间修改日期

这种事情是否可以使用 LINQ,语法是什么样的?

【问题讨论】:

    标签: linq


    【解决方案1】:

    是的。语法类似于:

    var files = from file in new DirectoryInfo(@"c:\some_folder")
                                .GetFiles("*.abc", SearchOption.AllDirectories)
    
                let lengthInKb = file.Length / 1024D
                let name = file.Name
                let modifiedDate = file.LastWriteTime.Date
    
                where  (lengthInKb >= x && lengthInKb <= y)
                    && (name.EndsWith("this") || name.Contains("that")) 
                    && !name.Contains("somethingelse")
                    && (modifiedDate >= date1 && modifiedDate <= date2)
    
               select file;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 2021-09-06
      相关资源
      最近更新 更多