directory info get files show directories C# .Net
There is sometimes a misunderstanding between a “file” and a “folder” in filesystems. In C# .Net the following is often used to list all “files” in a folder.
DirectoryInfo di = new DirectoryInfo("yourpath"); foreach (FileInfo fi in di.GetFiles()) { //do work }
However, even though you can specify to search containing subdirectories, the above function will not inherently list folders. If you are looking for the equivalent to “dir” or “ls”, instead use “GetFileSystemInfos()”.
DirectoryInfo di = new DirectoryInfo("yourpath"); //note the difference here with getfilesysteminfos foreach (dynamic d in di.GetFileSystemInfos()) { }
Note the usage of dynamic in the above example compared to the first example. This avoids any potential issues with inheritance and choosing the right class for your temp iterator variable for unboxing etc.
Posted on June 25, 2013, in Programming & Development and tagged .net, asp, browse, c#, command, dev, development, dir, directory, file, filesystem, io, ls, msdn, system.io, system.io.file. Bookmark the permalink. Leave a comment.
Leave a comment
Comments 0