首页 > 代码库 > 得到目录下的所有文件

得到目录下的所有文件


 

 //得到目录下的所有文件夹

public static void Get_All_Filepath_In_Directory(string dirpath, List<string> listpaths)
        {
            List<string> filePaths = listpaths;   //存放文件的路径
            try
            {
                if (Is_Directory(dirpath))   //判断是否存在目录
                {

                    filePaths.AddRange(Directory.GetFiles(dirpath).ToList());   //Directory.GetFiles(dirpath)得到目录下所有的文件
                    List<string> listDir = Directory.GetDirectories(dirpath).ToList();  //Directory.GetDirectories(dirpath)  得到目录下的所有子目录
                        foreach (var item in listDir)
                        Get_All_Filepath_In_Directory(item, filePaths);  //递归调用
                }

            }
            catch (Exception ex)
            {
                  //code...
            }
            finally
            {
                 //code...
            }
        }

 public static bool Is_Directory(string path)  //判断目录是否存在
        {
            bool flag = false;
            try
            {
                if (!string.IsNullOrWhiteSpace(path))  //判断目录是否为null或者是空
                {
                    if (Directory.Exists(path))   //存在返回TRUE
                    {
                        return true;
                    }
                }
                return flag;
            }
            catch (Exception ex)
            {
                return false;
            }
        }