C# program to find files in a directory
hi, working on finding files directory instance .txt, .csv, .xls, .xlsx using below code. finds files of 1 extension , in specified directory , not traverse through sub-directories. how re-write search multiple file formats & traverse through sub-directories.
string[] dirs = directory.getfiles(@"c:\temp\", "*.csv"); foreach (string dir in dirs) { console.writeline(dir); }
thanks in advance....
sqlenthusiast
try filter linq
var files = directory.getfiles("c:\\temp", "*.*", searchoption.alldirectories).where(s => s.endswith("*.csv") || s.endswith("*.jpg"));
or
try create extension methoddinfo.getfilesbyextensions(".jpg",".exe",".gif");
public static ienumerable<fileinfo> getfilesbyextensions(this directoryinfo dir, params string[] extensions)
{
if (extensions == null)
throw new argumentnullexception("extensions");
ienumerable<fileinfo> files = dir.enumeratefiles();
return files.where(f => extensions.contains(f.extension));
}
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment