如何实现在C#中的glob [英] How to implement glob in C#

查看:105
本文介绍了如何实现在C#中的glob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否是合法的计算器到您自己的答案张贴到一个问题,但我看到没有人问过这个了。我去寻找一个C#水珠,并没有找到一个,所以我写了一个其他人可能会发现有用的。


解决方案

  ///<总结>
    ///返回匹配一些通配符模式的文件列表,例如
    /// C:\\ P4 \\ SOFTWARE \\ DOTNET \\工具\\ * \\ * SLN让所有工具的解决方案文件
    ///< /总结>
    ///< PARAM NAME =水珠>模式匹配和LT; /参数>
    ///<&回报GT;所有匹配的路径< /回报>
    公共静态的IEnumerable<串GT;水珠(串水珠)
    {
        的foreach(在水珠路径字符串(PathHead(水珠)+ DirSep,PathTail(水珠)))
            产量返回路径;
    }    ///<总结>
    ///用途头和尾 - 头已被图案膨胀
    ///和尾巴还没有。
    ///< /总结>
    ///< PARAM NAME =头>通配符扩大和LT; /参数>
    ///< PARAM NAME =尾巴>尚未通配符扩大和LT; /参数>
    ///<&回报GT;< /回报>
    公共静态的IEnumerable<串GT;水珠(字符串头,尾部字符串)
    {
        如果(PathTail(尾)==尾)
            的foreach(在Directory.GetFiles路径​​字符串(头,尾).OrderBy(S = GT S))
                产量返回路径;
        其他
            的foreach(在Directory.GetDirectories字符串目录(头,PathHead(尾))排序依据(S =方式> S))
                的foreach(在水珠(字符串路径Path.Combine(头,DIR),PathTail(尾)))
                    产量返回路径;
    }    ///<总结>
    ///快捷方式
    ///< /总结>
    静态字符DirSep = Path.DirectorySeparatorChar;    ///<总结>
    ///返回一个文件路径的第一元件
    ///< /总结>
    ///< PARAM NAME =路径>文件的路径和LT; /参数>
    ///<返回>首先逻辑单元LT; /回报>
    静态字符串PathHead(字符串路径)
    {
        // \\\\共享\\卷\\富\\栏的手柄情况下 - 返回\\\\共享\\卷为头
        //因为DIR东西不会让你询问一个服务器的共享列表
        //看到Linux的FIXME检查的行为,如果这打击了 - 我不这么认为
        如果(path.StartsWith(+ DirSep + DirSep))
            返回path.Substring(0,2)+ path.Substring(2).Split(DirSep)[0] + DirSep + path.Substring(2).Split(DirSep)[1];        返回path.Split(DirSep)[0];
    }    ///<总结>
    ///返回一切,但文件路径的第一元件
    例如/// PathTail(C:\\ TEMP \\ foo.txt的)=TEMP \\ foo.txt的
    ///< /总结>
    ///< PARAM NAME =路径>文件的路径和LT; /参数>
    ///<&回报GT;所有,但第一个逻辑单元LT; /回报>
    静态字符串PathTail(字符串路径)
    {
        如果(!path.Contains(DirSep))
            返回路径;        返回path.Substring(1+ PathHead(路径)。长度);
    }

I don't know if it's legit at StackOverflow to post your own answer to a question, but I saw nobody had asked this already. I went looking for a C# Glob and didn't find one, so I wrote one that others might find useful.

解决方案

    /// <summary>
    /// return a list of files that matches some wildcard pattern, e.g. 
    /// C:\p4\software\dotnet\tools\*\*.sln to get all tool solution files
    /// </summary>
    /// <param name="glob">pattern to match</param>
    /// <returns>all matching paths</returns>
    public static IEnumerable<string> Glob(string glob)
    {
        foreach (string path in Glob(PathHead(glob) + DirSep, PathTail(glob)))
            yield return path;
    }

    /// <summary>
    /// uses 'head' and 'tail' -- 'head' has already been pattern-expanded
    /// and 'tail' has not.
    /// </summary>
    /// <param name="head">wildcard-expanded</param>
    /// <param name="tail">not yet wildcard-expanded</param>
    /// <returns></returns>
    public static IEnumerable<string> Glob(string head, string tail)
    {
        if (PathTail(tail) == tail)
            foreach (string path in Directory.GetFiles(head, tail).OrderBy(s => s))
                yield return path;
        else
            foreach (string dir in Directory.GetDirectories(head, PathHead(tail)).OrderBy(s => s))
                foreach (string path in Glob(Path.Combine(head, dir), PathTail(tail)))
                    yield return path;
    }

    /// <summary>
    /// shortcut
    /// </summary>
    static char DirSep = Path.DirectorySeparatorChar;

    /// <summary>
    /// return the first element of a file path
    /// </summary>
    /// <param name="path">file path</param>
    /// <returns>first logical unit</returns>
    static string PathHead(string path)
    {
        // handle case of \\share\vol\foo\bar -- return \\share\vol as 'head'
        // because the dir stuff won't let you interrogate a server for its share list
        // FIXME check behavior on Linux to see if this blows up -- I don't think so
        if (path.StartsWith("" + DirSep + DirSep))
            return path.Substring(0, 2) + path.Substring(2).Split(DirSep)[0] + DirSep + path.Substring(2).Split(DirSep)[1];

        return path.Split(DirSep)[0];
    }

    /// <summary>
    /// return everything but the first element of a file path
    /// e.g. PathTail("C:\TEMP\foo.txt") = "TEMP\foo.txt"
    /// </summary>
    /// <param name="path">file path</param>
    /// <returns>all but the first logical unit</returns>
    static string PathTail(string path)
    {
        if (!path.Contains(DirSep))
            return path;

        return path.Substring(1 + PathHead(path).Length);
    }

这篇关于如何实现在C#中的glob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆