获取指定文件夹下所有文件

///<summary>

        ///要显示的文件扩展名

        ///</summary>

        ///<param name="type"></param>

        ///<returns></returns>

        public bool checkFileType(string type)

        {

            bool FileType = false;

            string[] type_ = new string[4];

            type = type.ToLower();

            type_[0] = ".jpg";

            type_[1] = ".gif";

            type_[2] = ".jpeg";

            type_[3] = ".png";

            //可在此添加上传文件的后缀名

            for (int i = 0; i < type_.Length; i++)

            {

                if (type.Contains(type_[i].ToString()))

                {

                    FileType = true;

                }

            }

            return FileType;

        }

        ///<summary>

        ///遍文件夹下的所有子文件夹下的文件

        ///</summary>

        ///<param name="ObjDirPath">文件夹</param>

        public void GetFiles(string ObjDirPath)

        {

            DirectoryInfo SourceDir = new DirectoryInfo(ObjDirPath);

 

            foreach (FileSystemInfo FSI in SourceDir.GetFileSystemInfos())

            {

                if (FSI is DirectoryInfo)

                {

                    //如果是文件夹则递归

                    GetFiles(FSI.FullName);

                }

                else

                {

                    //如果是符合要求的文件则垒加集合,因为我只要求显示图片文件,在checkFileType方法里定义要显示文件的扩展名

                    if (checkFileType(FSI.Extension))

                    {

                        //由于是物理路径,如e:/luobing_web/uploadfiles/picture/test.jpg这种形式,需要提取虚拟路径,如:../uploadfiles/picture/test.jpg

                        string FilePath = ""; //一步写来看起混乱,就分开写了

                        FilePath = FSI.FullName.ToLower();

                        FilePath = FilePath.Substring(FilePath.LastIndexOf("uploadfiles//"));

                        FilePath = "../"+FilePath.Replace("//", "/");//这里在路径前加了../,因为我的项目里页面文件和上传文件夹不是同级文件夹

                        File_List += FilePath + ",";

                    }

                }

            }

        }

你可能感兴趣的:(list,String,File,扩展)