得到目录下的所有文件


 

 //得到目录下的所有文件夹  dirpath是目录路径  listpaths是用于保存文件全路径

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;
            }
        }
posted @ 2014-07-07 20:38  荣码一生  阅读(212)  评论(0编辑  收藏  举报