【问题标题】:how to EnumerateFiles use in 3.5 framework如何在 3.5 框架中使用 EnumerateFiles
【发布时间】:2016-05-27 06:36:25
【问题描述】:

我有以下查询如何在 3.5 框架中使用此查询。因为我不能改变我的框架请帮助我。我非常感谢你

 var files = from file in Directory.EnumerateFiles(path, "*.xml", SearchOption.AllDirectories)
                                from line in File.ReadLines(file)
                                where line.Contains("2016-05-09 10:31:28:000 +0100")
                                select new
                                {
                                    File = file,
                                    Line = line
                                }

;

【问题讨论】:

    标签: c# winforms linq linq-to-xml


    【解决方案1】:

    根据documentation on msdn,EnumerateFiles 从 .NET Framework 4.0 开始可用。

    您需要将其替换为 Directory.GetFiles

    var files = from file in Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories)
                from line in File.ReadLines(file)
                where line.Contains("2016-05-09 10:31:28:000 +0100")
                select new
                {
                    File = file,
                    Line = line
                }
    

    您只需要注意区别 - GetFiles 不是惰性的,因此它会立即搜索所有文件路径并将其加载到将通过 linq 查询的数组中。

    【讨论】:

    • Error2 'System.IO.File' 不包含 'ReadLines' 的定义
    • 你修复ReadLines了吗?它在 4.0 中也不可用。但是有ReadAllLines
    猜你喜欢
    • 1970-01-01
    • 2013-07-30
    • 2011-08-05
    • 1970-01-01
    • 2012-02-22
    • 2010-12-13
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    相关资源
    最近更新 更多