C# 获取文件夹中所有指定后缀名的文件名称

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Pdf2ImageProcess
{
    public partial class FileGet
    {
        ///


        /// 私有变量
        ///

        private static List lst = new List();
        ///
        /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
        ///

        /// 文件夹路径
        /// 扩展名可以多个 例如 .mp3.wma.rm
        /// List
        public static List getFile(string path, string extName)
        {
            getdir(path, extName);
            return lst;
        }
        ///
        /// 私有方法,递归获取指定类型文件,包含子文件夹
        ///

        ///
        ///
        private static void getdir(string path, string extName)
        {
            try
            {
                string[] dir = Directory.GetDirectories(path); //文件夹列表   
                DirectoryInfo fdir = new DirectoryInfo(path);
                FileInfo[] file = fdir.GetFiles();
                //FileInfo[] file = Directory.GetFiles(path); //文件列表   
                if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空                   
                {
                    foreach (FileInfo f in file) //显示当前目录所有文件   
                    {
                        if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
                        {
                            lst.Add(f);
                        }
                    }
                    foreach (string d in dir)
                    {
                        getdir(d, extName);//递归   
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }


    public partial class FileGet1
    {
        ///


        /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
        ///

        /// 文件夹路径
        /// 扩展名可以多个 例如 .mp3.wma.rm
        /// List
        public static List getFile(string path, string extName)
        {
            try
            {
                List lst = new List();
                string[] dir = Directory.GetDirectories(path); //文件夹列表   
                DirectoryInfo fdir = new DirectoryInfo(path);
                FileInfo[] file = fdir.GetFiles();
                //FileInfo[] file = Directory.GetFiles(path); //文件列表   
                if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空                   
                {
                    foreach (FileInfo f in file) //显示当前目录所有文件   
                    {
                        if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
                        {
                            lst.Add(f);
                        }
                    }
                    foreach (string d in dir)
                    {
                        getFile(d, extName);//递归   
                    }
                }
                return lst;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
 

使用实例:

 string strPath = @"E:\工具类\C#\pdf2word\pdf2image\bin\Temp\7be32129-202c-4828-b271-6faf32c91569\Scanned";
  List lstFiles = FileGet1.getFile(strPath, ".jpg");

 

你可能感兴趣的:(C#,获取文件名,文件名)