Access directories and subdirectories in ASPNET CORE MVC

sblb 1,166 Reputation points

Hi, I want to access all the files and subdirectories of a file table (Sql).
I can access the root directory "WebApiUploads_Dir" and return all the files on my interface.
However, I don't see all the subdirectories and I would like to access them to see the content


        public async Task<IActionResult> GetAsync()
            string directory = await _fileRepository.GetFileTableDirectory("WebApiUploads_Dir");
            string[] files = Directory.GetFiles(directory).Select(f => Path.GetFileName(f)).ToArray();

            return Ok(files);

I can acces to subdirectory if I had ("WebApiUploads_Dir\Subdir1");

But In this case I see only the content of the Subdir1
Plus I have ten subdir.

The definition of GetFileTableDirectory

   public async Task<string> GetFileTableDirectory(string fileStreamDirectory)
            FileTableRoot results = await _context.Set<FileTableRoot>().FromSqlRaw("SELECT FileTableRootPath() as Name").FirstOrDefaultAsync();
            return $"{results?.Name}\\{fileStreamDirectory}";

Can you some advise to achieve it or some example?
Thanks in advance

A set of technologies in the .NET Framework for building web applications and XML web services.
4,227 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,161 Reputation points

    The Directory.GetDirectories Method returns all subdirectories in a directories.

    The Directory.GetFiles Method returns all files within a directory.

    With these two methods you should be able to walk the directory structure. Or you can do a Google search for tons of examples.

    Edit: The file table identifies files and directories. A basic query can fetch the paths.

    0 comments No comments

11 additional answers

Sort by: Most helpful
  1. sblb 1,166 Reputation points

    Hi, thanks to your links.
    It's Ok. I was able to structure the directories and sub-directories

    For your information I started from the beginning and structured my code differently.
    Once again, thank you for your advices.

    One last thing, in the name column there is the name of the file with its extension. Do you know how I can do to return the name without the extension. Because I would like to put the file extension in another column.

    I still don't understand why the files don't open directly on my web page and go through the file download first. I'll look into it.

  2. sblb 1,166 Reputation points


    I've implemented the data in File Table as follow.

    Access to the directory via the web interface is very slow.
    On the other hand once I am in the directory the files open relatively quickly.
    for example in the directory 300 I have 10k files.
    How can I improve the access to the directory?
    When I am on the server in the table directory the access is almost immediate.
    Do you have any suggestions for fast access to directories via the web interface?
    Do I have to set up FileTable in a specific way?

    0 comments No comments

  3. AgaveJoe 26,161 Reputation points

    First, quantify "very slow". Second, are you sure the perceived "very slow" is due to listing the files in a directory and not the amount of data being transferred over the wire? Or maybe the transfer is reasonable but your UI has problems? Frankly, displaying 10,000 file is a lot data on the screen for a user to scroll through. Usually, you'll want to limit the result set by paging and/or filtering.

    0 comments No comments

  4. sblb 1,166 Reputation points

    There is indeed a pagination and filter in my display. TThe user searches for a file in the search box.

    Or maybe the transfer is reasonable but your UI has problems?

    What kind of problems can be seen on the UI?

    0 comments No comments