zoukankan      html  css  js  c++  java
  • C#递归搜索指定目录下的文件或目录

    https://www.cnblogs.com/huhangfei/p/5012978.html

    诚然可以使用现成的Directory类下的GetFiles、GetDirectories、GetFileSystemEntries这几个方法实现同样的功能,但请相信我不是蛋疼,原因是这几个方法在遇上【System Volume Information】这种目录时,极有可能会给你个拒绝访问的异常,想跳过都不行。所以没办法,重新实现了一下。
    
    实现说明
    
    • 仍然是基于对Directory类的几个方法的封装进行实现,只是没有使用它们的searchPattern和searchOption功能
    • 将匹配模式由windows的通配符?、改为正则匹配。一是让匹配更强大,二是要实现?匹配还得做额外工作,没必要
      匹配模式并没有默认添加首尾限定$,即“abc"将会匹配所有包含该字串的项目,所以如果你要匹配首尾,请自行添加$
      忽略大小写匹配
      如果不想搜索指定项目而是全部,请将regexPattern参数设为null,而不是.*,前者性能更好
    • 可通过设置recurse和throwEx参数决定是否递归搜索,和是否抛异常。默认是不递归,不抛异常
    • 遇到不可访问的目录会跳过。当然前提是throwEx=false
    • 之所以在foreach外层再套一层try-catch,是因为如果指定的dir就是不可访问的目录,那也可以避免异常
    • 之所以为获取项、获取文件、获取目录分别实现3个方法,而不是只实现一个获取项,另外两个重载,是因为只实现一个的话,foreach中要做的逻辑判断不少,考虑到方法是要递归的,所以循环中逻辑越少越好
    • 之所以不做dir参数合法性检查,原因同上,判断越少越好。所以请用户调用前自行确保dir合法

    不废话,上代码:

    复制代码
    /// <summary>
    /// 获取指定目录中的匹配项(文件或目录)
    /// </summary>
    /// <param name="dir">要搜索的目录</param>
    /// <param name="regexPattern">项名模式(正则)。null表示忽略模式匹配,返回所有项</param>
    /// <param name="recurse">是否搜索子目录</param>
    /// <param name="throwEx">是否抛异常</param>
    /// <returns></returns>
    private static string[] GetFileSystemEntries(string dir, string regexPattern = null, bool recurse = false, bool throwEx = false)
    {
        List<string> lst = new List<string>();
    
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
    {
        </span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">string</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> Directory.GetFileSystemEntries(dir))
        {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
            {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (regexPattern == <span style="color: rgba(0, 0, 255, 1)">null</span> || Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase |<span style="color: rgba(0, 0, 0, 1)"> RegexOptions.Multiline))
                { lst.Add(item); }
    
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">递归</span>
                <span style="color: rgba(0, 0, 255, 1)">if</span> (recurse &amp;&amp; (File.GetAttributes(item) &amp; FileAttributes.Directory) ==<span style="color: rgba(0, 0, 0, 1)"> FileAttributes.Directory)
                { lst.AddRange(GetFileSystemEntries(item, regexPattern, </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">)); }
            }
            </span><span style="color: rgba(0, 0, 255, 1)">catch</span> { <span style="color: rgba(0, 0, 255, 1)">if</span> (throwEx) { <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)">; } }
        }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">catch</span> { <span style="color: rgba(0, 0, 255, 1)">if</span> (throwEx) { <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)">; } }
    
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> lst.ToArray();
    

    }

    /// <summary>
    /// 获取指定目录中的匹配文件
    /// </summary>
    /// <param name="dir">要搜索的目录</param>
    /// <param name="regexPattern">文件名模式(正则)。null表示忽略模式匹配,返回所有文件</param>
    /// <param name="recurse">是否搜索子目录</param>
    /// <param name="throwEx">是否抛异常</param>
    /// <returns></returns>
    private static string[] GetFiles(string dir, string regexPattern = null, bool recurse = false, bool throwEx = false)
    {
    List
    <string> lst = new List<string>();

    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
    {
        </span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">string</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> Directory.GetFileSystemEntries(dir))
        {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
            {
                </span><span style="color: rgba(0, 0, 255, 1)">bool</span> isFile = (File.GetAttributes(item) &amp; FileAttributes.Directory) !=<span style="color: rgba(0, 0, 0, 1)"> FileAttributes.Directory;
    
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (isFile &amp;&amp; (regexPattern == <span style="color: rgba(0, 0, 255, 1)">null</span> || Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase |<span style="color: rgba(0, 0, 0, 1)"> RegexOptions.Multiline)))
                { lst.Add(item); }
    
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">递归</span>
                <span style="color: rgba(0, 0, 255, 1)">if</span> (recurse &amp;&amp; !isFile) { lst.AddRange(GetFiles(item, regexPattern, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">)); }
            }
            </span><span style="color: rgba(0, 0, 255, 1)">catch</span> { <span style="color: rgba(0, 0, 255, 1)">if</span> (throwEx) { <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)">; } }
        }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">catch</span> { <span style="color: rgba(0, 0, 255, 1)">if</span> (throwEx) { <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)">; } }
    
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> lst.ToArray();
    

    }

    /// <summary>
    /// 获取指定目录中的匹配目录
    /// </summary>
    /// <param name="dir">要搜索的目录</param>
    /// <param name="regexPattern">目录名模式(正则)。null表示忽略模式匹配,返回所有目录</param>
    /// <param name="recurse">是否搜索子目录</param>
    /// <param name="throwEx">是否抛异常</param>
    /// <returns></returns>
    private static string[] GetDirectories(string dir, string regexPattern = null, bool recurse = false, bool throwEx = false)
    {
    List
    <string> lst = new List<string>();

    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
    {
        </span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">string</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> Directory.GetDirectories(dir))
        {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
            {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (regexPattern == <span style="color: rgba(0, 0, 255, 1)">null</span> || Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase |<span style="color: rgba(0, 0, 0, 1)"> RegexOptions.Multiline))
                { lst.Add(item); }
    
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">递归</span>
                <span style="color: rgba(0, 0, 255, 1)">if</span> (recurse) { lst.AddRange(GetDirectories(item, regexPattern, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">)); }
            }
            </span><span style="color: rgba(0, 0, 255, 1)">catch</span> { <span style="color: rgba(0, 0, 255, 1)">if</span> (throwEx) { <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)">; } }
        }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">catch</span> { <span style="color: rgba(0, 0, 255, 1)">if</span> (throwEx) { <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)">; } }
    
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> lst.ToArray();
    

    }

    复制代码
  • 相关阅读:
    数学归纳法证明等值多项式
    整值多项式
    同余式
    欧拉定理&费马定理
    与模互质的剩余组
    欧拉函数的性质
    欧拉函数计数定理
    完全剩余组高阶定理
    51nod 1488 帕斯卡小三角 斜率优化
    51nod 1577 异或凑数 线性基的妙用
  • 原文地址:https://www.cnblogs.com/sunny3158/p/14359268.html
Copyright © 2011-2022 走看看