本文介绍了如何使用通配符扫描具有特定子目录的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是扫描具有不确定字符的目录的好方法。



例如,我想扫描

含义




  • 该文件夹位于 C:\Program

  • Version2。* 可以是任何类似 Version2.33 Version2.1 等。

  • 该文件夹中有一个名为 Files 的文件夹我知道如果包含(Version2),我可以像 foreach(directory)这样做一些东西。 ,但我想知道是否有更好的方式这样做。

    解决方案

    接受搜索模式。所以列举具有通配符的父项,而不是枚举其他的:

      var directories = 
    Directory.EnumerateDirectories(@C :\Program\,Version2。*)
    .SelectMany(parent => Directory.EnumerateDirectories(parent,Files))

    注意:如果路径可以包含任何级别的通配符 - 简单地标准化路径并按\拆分,而不是逐级收集文件夹。


    I was wondering what would be a good way to scan a directory that has characters you are not sure of.

    For example, I want to scan

    Meaning

    • The folder is located in C:\Program
    • Version2.* could be anything like Version2.33, Version2.1, etc.
    • That folder has a folder named Files in it

    I know that I could do something like foreach (directory) if contains("Version2."), but I was wondering if there was a better way of doing so.

    解决方案

    Directory.EnumerateDirectories accepts search pattern. So enumerate parent that has wildcard and than enumerate the rest:

      var directories =
        Directory.EnumerateDirectories(@"C:\Program\", "Version2.*")
         .SelectMany(parent => Directory.EnumerateDirectories(parent,"Files"))
    

    Note: if path can contain wildcards on any level - simply normalize path and split by "\", than collect folders level by level.

    这篇关于如何使用通配符扫描具有特定子目录的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 07:07