How to get filenames inside folders on WEB server API

3

I have a directory File / Uploads

AndIwouldliketolistallfiles,insidethisdirectoryIhaveNfolders.

WhatI'veachievedsofarishere.

stringpath=HttpContext.Current.Server.MapPath("~//Arquivos//Uploads");
            string[] files = System.IO.Directory.GetFiles(path);
    
asked by anonymous 08.01.2017 / 00:20

1 answer

3

Use EnumerateFiles to get the files from a given directory , setting the third parameter System.IO.SearchOption.AllDirectories so that the search is done in every directory.

string path = HttpContext.Current.Server.MapPath("~//Arquivos//Uploads");
IEnumerable<string> files = System.IO.Directory.EnumerateFiles(path,
                                      "*.*",
                                       System.IO.SearchOption.AllDirectories);

Note: System.IO.Directory.GetFiles " can also be used in the same way also having 3 same definition of System.IO.SearchOption.AllDirectories .

  

System.IO.Directory.GetFiles ( path , "% *.* );

Using Linq can work on this information and get only the name of the files:

var result = System.IO.Directory.EnumerateFiles(path, 
                      "*.*", 
                      System.IO.SearchOption.AllDirectories)
                .Select(c => 
                       c.Split(new string[] { "\" }, StringSplitOptions.None).Last())
                .ToArray();

References:

08.01.2017 / 00:39