开发者

How can I retrieve a collection of file locations if I give the method a location of the parent folder?

开发者 https://www.devze.com 2023-01-03 19:56 出处:网络
For example, giv开发者_如何学运维en the folder: C:\\images\\a How could I retrieve a collection of all of the images in that folder? Thank you.For just simple string paths, you can use the GetFiles s

For example, giv开发者_如何学运维en the folder: C:\images\a

How could I retrieve a collection of all of the images in that folder? Thank you.


For just simple string paths, you can use the GetFiles static method in the Directory class (System.IO).

string[] files = Directory.GetFiles(path, "*.jpg", SearchOption.AllDirectories);

For more information about the files, you could use the DirectoryInfo class to retrieve FileInfo objects about each file.

FileInfo[] fileInfos = new DirectoryInfo(path).GetFiles("*.jpg", SearchOption.AllDirectories);


Call Directory.GetFiles.

If you want to recursively search subfolders, pass SearchOption.AllDirectories.


One of the ways you could do it is:

DirectoryInfo di = new DirectoryInfo("c:/demos");
 FileInfo[] rgFiles = di.GetFiles("*.jpgs");
 foreach(FileInfo fi in rgFiles)
 {
  //do your stuff       
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号