C#中获取指定路径下指定后缀名的所有文件的路径的list

场景

指定一个路径和后缀名,查找这个路径下所有以此后缀名结尾的文件。

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

新建工具类FileHelper,工具类中新建方法GetFileListWithExtend

 public static List<string> GetFileListWithExtend(DirectoryInfo directory, string pattern)
        {
            List<string> pathList = new List<string>();
            string result = String.Empty;
            if (directory.Exists || pattern.Trim() != string.Empty)
            {

                foreach (FileInfo info in directory.GetFiles(pattern))
                {
                    result = info.FullName.ToString();
                    pathList.Add(result);
                }
            }
            return pathList;

        }

调用示例

List<string> taskFileList = FileHelper.GetFileListWithExtend(new DirectoryInfo(currentPath), "*.pcj");

其中currentpath就是一个指定的路径

然后第二个参数是指定后缀名

调用结果

 

原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/11890743.html