【问题标题】:Extracting specific information from IEnumerable using LINQ?使用 LINQ 从 IEnumerable 中提取特定信息?
【发布时间】:2021-03-04 20:21:49
【问题描述】:

我有一个包含文件和目录信息的 IEnumerable - 下面是它的内容示例:-

我想从此 IEnumerable 中提取特定信息,并假设 LINQ 是最佳途径

我想要给定路径中的文件列表和单独的目录列表 - 但只是那个级别,所以不包括子目录等

所以如果我通过 C:\ 那么它只返回 C:\ 根目录中的文件列表和 C:\ 根目录中的目录列表 - 仅 1 级。如果我通过 C:\Windows 则它返回 C:\Windows 根目录中的文件列表和 C:\Windows 中的目录列表 - 再次只是给定的级别,没有更低(或更高)

列表只需要包含每个文件/目录的全名即可

如果是目录,则 Attributes 值包含 Directory,因此我可以使用它来轻松确定文件或目录是否使用类似于下面的内容

Dim dirs2 = nodes.Where(Function(n) n.FullName.StartsWith(path) And n.Attributes = Attributes.Directory)
Dim files2 = nodes.Where(Function(n) n.FullName.StartsWith(path) And n.Attributes <> Attributes.Directory)

但我被困在如何只返回 FullName 值 - 以及 - 如何只返回一个级别

下面链接的示例项目显示了使用 NTFSreader link 填充 IEnumerable 的确切方式

注意我想继续使用 Ntfs 阅读器,而不是使用 IO.Directory.GetDirectories / GetFiles,因为 NTFS 阅读器要快得多,并且没有相同的权限问题,因为它直接从 MFT 获取信息

http://www.pcassistonline.co.uk/upload/test_source.zip

这里有人有什么想法吗?在 Google 和其他论坛上花了几个小时,但在这里不知所措 - 谢谢

【问题讨论】:

  • 如果您只需要FullName 属性,请选择它。 Dim dirs2 = nodes.Where(Function(n) n.FullName.StartsWith(path) AndAlso n.Attributes = Attributes.Directory).Select(Function(n) n.FullName)。对于顶层部分,回到你得到nodes 的地方,该方法应该有某种方式来指示它。检查过载可能。

标签: vb.net linq ienumerable


【解决方案1】:

你可以这样使用:

在此示例中,您可以将这两个信息作为 IEnumerableStructure DFInfo,其中有 NameFullName (为方便起见)

仅对于FullName,此代码应该更苗条,去掉DFInfoStructure。 请注意,没有必要使用Attributes(如您所示)来查看是File 还是Directory,因为GetDirectoriesGetFiles 方法仅返回目录或仅返回文件。

重要的是,当Security Exception 出现时,您无法通过受 SO 保护的 FileDirectory 获取信息。

希望就是你所需要的。

Private Enum DF
    Directory = 0
    Files = 1
End Enum

Structure DFInfo
    Dim Name As String
    Dim FullName As String
End Structure

Private Function GetDFFromPath(path As String, whatINeed As DF) As IEnumerable(Of DFInfo)
    Dim elements As List(Of String)

    If whatINeed = DF.Directory Then
        elements = IO.Directory.GetDirectories(path, "*.*", searchOption:=SearchOption.TopDirectoryOnly).ToList
    Else
        elements = IO.Directory.GetFiles(path, "*.*", searchOption:=SearchOption.TopDirectoryOnly).ToList
    End If

    If elements IsNot Nothing AndAlso elements.Count > 0 Then
        Return elements.ConvertAll(Function(str) New DFInfo With {.Name = New FileInfo(str).Name, .FullName = New FileInfo(str).FullName})
    End If

    Return Nothing

End Function

用法:

Dim allDirs As IEnumerable(Of DFInfo) = GetDFFromPath("C:\", DF.Directory)
Dim allFiles As IEnumerable(Of DFInfo) = GetDFFromPath("C:\Windows", DF.Files)

如果您想使用 NTFSReader 工作/改进以下 2 条说明:

    Dim rootDir = "C:\"
    Dim dirs2 As List(Of String) =
        nodes.Where(Function(n)
                        Return n.FullName = rootDir & n.Name And n.Attributes = Attributes.Directory
                    End Function).
    Select(Function(n) n.FullName).ToList

    rootDir = "C:\Windows\"
    Dim files2 As List(Of String) =
        nodes.Where(Function(n)
                        Return n.FullName = rootDir & n.Name And n.Attributes <> Attributes.Directory
                    End Function).
        Select(Function(n) n.FullName).ToList

【讨论】:

  • 嗨,感谢您的回复我不想使用 GetDirectories 或 GetFiles,因为 a)它太慢 b)您提到的安全问题这就是我使用 NTFSreader 的原因 - link -因为它非常快速地获取有关 NTFS 驱动器上所有文件/文件夹的信息所以根据我附加的示例应用程序,这将所有结果存储在我的案例节点中调用的 IEnumerable 中。所以我想从节点 IEnumerable 中提取我需要的所有信息,因此我想知道如何使用 LINQ 从中获取信息
  • @DarrenRose 使用这个:nodes.Where(Function(n) n.FullName.StartsWith(path) And n.Attributes = Attributes.Directory).Select(Function(s) New FileInfo(s).FullName) 所以.Select(Function(s) New FileInfo(s).FullName) 部分得到FullName。我不知道 NTFSreader 库
  • 但是,为了确保您仅在根路径中获取这些文件或目录,您已将这些文件或目录作为可用于文件的参数传递:… And (New IO.FileInfo(n.FullName).Directory = yourPathToExmine) And … 和目录:… And (New IO.DirectoryInfo(n.FullName).Parent.FullName = yourPathToExmine) And ...
  • 当节点 IEnumerable 已经包含我需要的所有信息时,我不确定为什么需要使用 FileInfo 和 DirectoryInfo?顺便说一下,第一个例子给出了一个错误。不确定您是否需要了解 NTFSreader 库,尽管通过我发布的示例很容易看到它提供的输出,但我认为它更像是一个 LINQ 专家
  • FileInfo 和 DirectoryInfo 是看元素是否没有嵌套(子目录)。
【解决方案2】:
Dim dirs As List(Of String) = nodes.Where(Function(n)
                             Return n.FullName = path & n.Name AndAlso ((n.Attributes And Attributes.Directory) = Attributes.Directory)
                             End Function).Select(Function(n) n.FullName).OrderBy(Function(n) n).ToList


Dim files As List(Of String) = nodes.Where(Function(n)
                               Return n.FullName = path & n.Name AndAlso ((n.Attributes And Attributes.Directory) <> Attributes.Directory)
                               End Function).Select(Function(n) n.FullName).OrderBy(Function(n) n).ToList

并且需要确保“路径”有一个尾随 \ 例如C:\ 或 C:\Windows\ 而不是 C:\Windows 好像不行一样

感谢所有输入 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-06
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2015-01-20
    • 2023-03-28
    • 2017-03-14
    相关资源
    最近更新 更多