In .Net 4 there are two functions to enumerate files in a path. The EnumerateFiles and EnumerateFileSystemEntries. The MSDN does not give enough explanations about their differences. Which are these differences?
EnumerateFileSystemEntries => enumerate files, directories, and both files and directories.
EnumerateFiles => for enumarting only files. (Not directories)
EnumerateFiles
returns only file names. EnumerateFileSystemEntries
returns names of directories also
As @Habib and @Sergey answered, you need to use EnumerateFileSystemEntries
instead of EnumerateFiles
. I will extend their answers with an example.
Here is a code to showcase the difference:
public static void Main()
{
Directory.CreateDirectory("dir");
Directory.CreateDirectory("dir2");
File.WriteAllText(Path.Combine("dir", Path.GetRandomFileName()), "file");
var datapath = ".";
var files = Directory.EnumerateFiles(datapath, "*", SearchOption.AllDirectories);
files.Dump();
var fileSystemEntries = Directory.EnumerateFileSystemEntries(datapath, "*", SearchOption.AllDirectories);
fileSystemEntries.Dump();
}
output
Dumping object(System.IO.FileSystemEnumerableIterator`1[String])
[
.\dir\epp3114y.jjr
]
Dumping object(System.IO.FileSystemEnumerableIterator`1[String])
[
.\dir
,
.\dir2
,
.\dir\epp3114y.jjr
]
Try it Online!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With