Specially added as a new answer..
Since .NET 2.0 There is IENumerable and yield keyword does Lazy Initialization/deferred execution..With these, you can get your wants.
public IEnumerable<string> GetFiles(string rootPath, string [] fileNameStartChars, string[] extensionsFilter) { FileSystemInfo[] fsi = null; for(int i = 0; i < fileNameStartChars.Length; i++) { for(int k = 0; k<extensionsFilter.Length; k++) { fsi = new DirectoryInfo(rootPath).GetFileSystemInfos(fileNameStartChars[i]+extensionsFilter[k]); if (fsi.Length > 0) { for (int j = 0; j < fsi.Length; j++) { /// .Name returns the filename with extension..if you need, please implement here a substring for eliminate the extension of the file yield return fsi[j].Name; } } } } }
And usage :
possible filenames startsWithChar table
public string[] table = new string[] { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "1","2","3","4","5","6","7","8","9","0","#","_","-",".","@","+",",","%","&","(",")","[","]","{","}","*", "<",">","^"," ","|",";","`" };
And extensions :
string[] Exts = new string[] { ".mp3", ".midi", ".wav"};
with this method, you can filter your data within small parts as such as using startswithchar filtering, so you won't get Memory problem which depends to your files count..This is the tricky part of trying to imitate .net v4's EnumerateFiles method with 100% .net v2 managed code..
IEnumerable<string> strNumerable = GetFiles(@"D:\Music", table, Exts); ///Since its deferred execution, method didn't get any memory alloc for your data till now..Memory Alloc will start within this foreach.. foreach (string s in strNumerable) { //do your work }
Directory.GetFilesis the wrong place to optimize. You could optimize the processing method or you could load all paths withGetFiles, then process one part after the other, so for example every 10th file.Directory.GetFiles(path)first even if the iterator yields one after the other. So no, that just fakes deferred execution for a single directory with a large number of files.GetFilesexpensive? Or is it only expensive because you search subdirectories? In the latter case you can simply write the recursive descent yourself and you only incur the cost of a shallow search before starting the search. One implementation is at Enumerating Files Throwing ExceptionFindFirstFile,FindNextFile, etc..