C#中獲取指定路徑下指定後綴名的全部文件的路徑的list

場景

指定一個路徑和後綴名,查找這個路徑下全部以此後綴名結尾的文件。編程

注:工具

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。spa

實現

新建工具類FileHelper,工具類中新建方法GetFileListWithExtend.net

 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就是一個指定的路徑code

而後第二個參數是指定後綴名blog

調用結果

 

相關文章
相關標籤/搜索