Access directories and subdirectories in ASPNET CORE MVC

sblb 1,166 Reputation points
2022-03-22T12:33:21.863+00:00

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

Controller

   [HttpGet]
        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

ASP.NET Core
ASP.NET Core
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
    2022-03-22T15:52:41.987+00:00

    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.

    https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-iterate-through-a-directory-tree

    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
    2022-03-24T09:20:13.31+00:00

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

    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
    2022-03-30T15:24:52.687+00:00

    Hi,

    I've implemented the data in File Table as follow.
    188491-capture.jpg

    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
    2022-03-30T16:31:43.993+00:00

    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
    2022-03-30T17:05:04.417+00:00

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

    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