How can I retrieve a collection of file locations if I give the method a location of the parent folder?
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
}
精彩评论