Search code examples
asp.netcontent-management-systemumbraco

Umbraco: Is there a way to get the list of folder under the Media (Physical Folder)


I am just trying to create a list (textual value only) of folders under the Media folder (Physical Folder).

Media folder is located at the root folder.(See the image)

Umbraco root folder

I tried the following code just to test if I can get the last folder:

var contentService = Services.ContentService;
var folderList = contentService.GetRootContent().Last();
IContent s = folderList;

string nameOfLastFolder = folderList.Name.ToString();

But it returned Home

I also tried the following code:

var mediaService = Services.MediaService;
var folderList = mediaService.GetRootMedia().Last();
IMedia s = folderList;

string nameOfLastFolder = folderList.Name.ToString();

But it returned the name of the last media file which is correct if what I am trying to get is the media in the backstage but not the physical one. (See the following image)

Media folder

What I want to get is the list of media folder physically, not in the the backstage view media. I want to create a list of the physical media folder:

Physical Media Folder

I Hope that you can help me with this. Thanks.


Solution

  • You should loop through the folders in the filesystem instead of using the Umbraco service (which lists the database entries).

    Example

    string pathToFiles = HostingEnvironment.MapPath("/Media");
    string[] fileList = Directory.GetFiles(pathToFiles);