Access directories and subdirectories in ASPNET CORE MVC

sblb 1,231 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

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 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. AgaveJoe 30,126 Reputation points
    2022-03-30T18:30:27.277+00:00

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

    I can't see your code. Are you filtering the UI after fetching 10,000 records or before?

    What kind of problems can be seen on the UI?

    Returning 10,000 records directly to a JavaScript library could take a long time to render. You are the only one that knows how the code works and you are the only one that has access to the HTTP stream. Swagger shows the content-length and your browser's dev tools shows how long it takes to download content. I'm confused why you don't take a few minutes to look. The bottleneck should be obvious.

    0 comments No comments

  2. sblb 1,231 Reputation points
    2022-04-01T10:21:01.387+00:00

    I can't see your code. Are you filtering the UI after fetching 10,000 records or before?

    Yes, the filtering is made after fetching data.
    I suppose that I've to implement the filetering in code. Have you some advise?


  3. sblb 1,231 Reputation points
    2022-04-01T12:15:24.597+00:00

    Thanks to your remarks I ask me a question.

    I call the datable where the filter is already implemented. so why I 've to the filter in code?

    Other : I added in my code the action below

     public async Task<IActionResult> GetAsync()
            {
                string directory = await _fileRepository.GetFileTableDirectory("WebApiUploads_Dir");
                /
                string[] files = Directory.GetFiles(directory)
                                          .Select(f => Path.GetFileName(f))
                                          .ToArray();
    
                string[] subdir = Directory.GetDirectories(directory, ".", SearchOption.TopDirectoryOnly)
                                                         .Select(d => Path.GetFileName(d))
                                                         .ToArray();
    
               var tab = files.Union(subdir).ToArray();
    
                return Ok(tab);
    
            }
    

    But I would like to know how I can fill the directories with the files


  4. AgaveJoe 30,126 Reputation points
    2022-04-01T13:17:28.107+00:00

    I call the datable where the filter is already implemented. so why I 've to the filter in code?

    I do not know if the Datatable is the bottleneck. You have not shared the code so we have no idea how you've implemented the Datatable. I assume the MVC View renders an entire HTML table.

    The Datatable starts processing the HTML table after the browser loads the page and initializes the Datatable. Processing 10,000 rows it's going to take time. Logically reducing the number of records will also reduce the time it takes to process the HTML and why I recommend reducing the number of records returned from the service.

    The browser's dev tools has diagnostics tools that shows how long it takes to fetch a Web API response and how long it takes the DataTable to process the HTML.

    https://developer.chrome.com/docs/devtools/network/
    https://developer.chrome.com/blog/devtools-timeline-now-providing-the-full-story/

    With this information you should be able to find the bottleneck. Another troubleshooting option is disabling the DataTable library to see if the page is loads quicker. Then you'll know if the DataTable is the bottleneck or if you need to look elsewhere.

    For example, if you find the time it takes to transfer the data over the wire is the bottleneck then you can look into caching the data. Caching might be a good idea regardless.

    Cache in-memory in ASP.NET Core

    Lastly, you'll want to read the Datatable support documentation and learn how to make AJAX request to populate the Datatable and control the data.

    I have already explained to you that I am a beginner with c# with VS. please check my package with R.

    What I've described above is basic troubleshooting that applies to every programming endeavor.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.