C# 获取目录下的所有文件包括子目录

1、递归获取目录下的所有文件清单

    调用:

List<string> fileList = GetFileList(path);

   方法

private static List<string> GetFileList(string path)
        {
            List<string> fileList = new List<string>();

            if (Directory.Exists(path) == true) {
                foreach (string file in Directory.GetFiles(path)) {
                    fileList.Add(file);
                }

                foreach (string directory in Directory.GetDirectories(path)) {
                    fileList.AddRange(GetFileList(directory));
                }
            }

            return fileList;
        }

 

2、非递归获取目录下的文件清单

 
static List<string> fileList1 = new List<string>();
        private static void GetFileList1(string path)
        {
            if (Directory.Exists(path) == true) {
                foreach (string file in Directory.GetFiles(path)) {
                    fileList1.Add(file);
                }

                foreach (string directory in Directory.GetDirectories(path)) {
                    GetFileList1(directory);
                }
            }
        }
View Code
posted @ 2021-05-18 14:41  學海無邊  阅读(622)  评论(0编辑  收藏  举报