C#中:获取文件夹中的所有图像文件 [英] C#:Getting all image files in folder

查看:1747
本文介绍了C#中:获取文件夹中的所有图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件夹中获得的所有图像,但是,这个文件夹包括子文件夹。像/照片/ PERSON1 /和/照片/ PERSON2 /。我可以在文件夹中的照片像

I am trying to get all images from folder but ,this folder also include sub folders. like /photos/person1/ and /photos/person2/ .I can get photos in folder like

  path= System.IO.Directory.GetCurrentDirectory() + "/photo/" + groupNO + "/";
 public List<String> GetImagesPath(String folderName)
    {

        DirectoryInfo Folder;
        FileInfo[] Images;

        Folder = new DirectoryInfo(folderName);
        Images = Folder.GetFiles();
        List<String> imagesList = new List<String>();

        for (int i = 0; i < Images.Length; i++)
        {
            imagesList.Add(String.Format(@"{0}/{1}", folderName, Images[i].Name));
           // Console.WriteLine(String.Format(@"{0}/{1}", folderName, Images[i].Name));
        }


        return imagesList;
    }



但如何我能得到的所有照片中的所有子文件夹?我的意思是我想在/照片/目录中的所有照片一次。

But how can I get all photos in all sub folders? I mean I want to get all photos in /photo/ directory at once.

推荐答案

有一个看的DirectoryInfo.GetFiles 重载需要的 SearchOption 参数,并通过SearchOption.AllDirectories获得包括所有子目录中的文件。

Have a look at the DirectoryInfo.GetFiles overload that takes a SearchOption argument and pass SearchOption.AllDirectories to get the files including all sub-directories.

另一种选择是使用 Directory.GetFiles 具有过载,需要一个<一个HREF =http://msdn.microsoft.com/en-us/library/ms143448.aspx> SearchOption 参数,以及:

Another option is to use Directory.GetFiles which has an overload that takes a SearchOption argument as well:

return Directory.GetFiles(folderName, "*.*", SearchOption.AllDirectories)
                .ToList();

这篇关于C#中:获取文件夹中的所有图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆