Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net core 1 list files in given directory

I need to list all files that are present in a given folder, using C# for ASP.Net core 1. Something like System.IO.Directory.GetFiles() in earlier versions.

like image 900
Bilal Sehwail Avatar asked May 31 '16 13:05

Bilal Sehwail


People also ask

How do I enumerate a file?

To enumerate directories and files, use methods that return an enumerable collection of directory or file names, or their DirectoryInfo, FileInfo, or FileSystemInfo objects. If you want to search and return only the names of directories or files, use the enumeration methods of the Directory class.

Where are .NET core files stored?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method.

How do I read a folder in .NET core?

First, the File is read as Binary Data into a Byte Array object using the ReadAllBytes method of the File class. And then the Byte Array object is sent for download using the File function. //Fetch all files in the Folder (Directory). string[] filePaths = Directory.


1 Answers

you can do something like this:

foreach (string file in Directory.EnumerateFiles(
            pathToFolder, 
            "*" , 
            SearchOption.AllDirectories) 
            )
        {
            // do something

        }

note that I'm recursing child directories too which may or may not be what you want

like image 98
Joe Audette Avatar answered Oct 04 '22 18:10

Joe Audette