C#Directory-Searchpattern Subdirectorie(s)

时间:2017-10-09 15:34:26

标签: c# .net path directory

如何在C#中搜索类似的路径:

" C:\ MyApp的\ * \登录"

我希望获得与该搜索模式匹配的所有目录。

示例结果:

C:\ MyApp的\ 20171009 \日志
C:\ MyApp的\ 20171008 \日志
C:\ MyApp的\ 20171007 \日志

在Powershell中,它适用于get-item

3 个答案:

答案 0 :(得分:2)

尝试使用基于迭代器的文件函数:

var path = @"C:\temp";
foreach (var file in Directory.EnumerateFiles(path, "*.log", SearchOption.AllDirectories))
{
    Console.WriteLine(file);
}

有关更多信息,请显示here

答案 1 :(得分:0)

如果您尝试仅使用名称日志获取与模式C:\ MyApp * \ log匹配的目录,则以下代码应该有所帮助:

      TableName: "sdfdsgfdg"
      IndexName: 'username-category-index',
      KeyConditions: {
        "username": {
          "AttributeValueList": { "S": "aaaaaaa@gmail.com" }
          ,
          "ComparisonOperator": "EQ"
        },
        "username": {
          "AttributeValueList": { "S": "hhhhh@gmail.com" }
          ,
          "ComparisonOperator": "EQ"
        },
        "category": {
          "AttributeValueList": { "S": "Coupon" }
          ,
          "ComparisonOperator": "EQ"
        }
      }

请注意,搜索模式是目录的名称,而不是任何文件名或文件扩展名

答案 2 :(得分:0)

我为我的问题找到了solution

我将其修改为目录使用。

public static List<string> GetAllMatchingPaths(string pattern)
        {
            char separator = Path.DirectorySeparatorChar;
            string[] parts = pattern.Split(separator);

            if (parts[0].Contains('*') || parts[0].Contains('?'))
                throw new ArgumentException("path root must not have a wildcard", nameof(parts));

            return GetAllMatchingPathsInternal(String.Join(separator.ToString(), parts.Skip(1)), parts[0]);
        }

        private static List<string> GetAllMatchingPathsInternal(string pattern, string root)
        {
            char separator = Path.DirectorySeparatorChar;
            string[] parts = pattern.Split(separator);

            for (int i = 0; i < parts.Length; i++)
            {
                // if this part of the path is a wildcard that needs expanding
                if (parts[i].Contains('*') || parts[i].Contains('?'))
                {
                    // create an absolute path up to the current wildcard and check if it exists
                    var combined = root + separator + String.Join(separator.ToString(), parts.Take(i));
                    if (!Directory.Exists(combined))
                        return new List<string>();

                    if (i == parts.Length - 1) // if this is the end of the path (a file name)
                    {
                        return ( List<string> ) Directory.EnumerateFiles(combined, parts[i], SearchOption.TopDirectoryOnly);
                    }
                    else // if this is in the middle of the path (a directory name)
                    {
                        var directories = Directory.EnumerateDirectories(combined, parts[i], SearchOption.TopDirectoryOnly);

                        List<string> pts = new List<string>();
                        foreach ( string directory in directories )
                        {
                            foreach ( string item in GetAllMatchingPathsInternal(String.Join(separator.ToString(), parts.Skip(i + 1)), directory))
                            {

                                pts.Add(item);
                            }

                        }

                        return pts;
                    }
                }
            }