How to read all files that are hosted by a file server

135 views Asked by At

Desription

I am using FileServerOptions to host some files.

The EnableDirectoryBrowsing option is enabled so I can view the files through a browser.

I would like to get a list of all the files within the directory (preferably recursively).

Code

Server

The following code is used to serve the files.

public void Run(IAppBuilder appBuilder)
{
    var serviceProvider = IocStartup.BuildServiceProvider();
         
    config.DependencyResolver = new DefaultDependencyResolver(serviceProvider);
    
    appBuilder.UseWebApi(config);
    
    var fileServerOptions = new FileServerOptions
    {
        FileSystem  = new PhysicalFileSystem(SystemConfiguration.ConfigDirectory),
        RequestPath = new PathString("/Config"),
        EnableDirectoryBrowsing = true
    };

    appBuilder.UseFileServer(fileServerOptions);
}

Client

The following code can be used to read in the contents of an index file.

var indexFileStream = WebRequest.Create(indexFilePath).GetResponse().GetResponseStream();
if (indexFileStream != null)
{
    var streamReader = new StreamReader(indexFileStream, Encoding.UTF8);
    var indexFileContents = streamReader.ReadToEnd();
    streamReader.Close();
}

Output (Index File)

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Index of /Config/MainFolder/SubFolder</title>
  <style>
    body {
        font-family: "Segoe UI", "Segoe WP", "Helvetica Neue", 'RobotoRegular', sans-serif;
        font-size: 14px;}
    header h1 {
        font-family: "Segoe UI Light", "Helvetica Neue", 'RobotoLight', "Segoe UI", "Segoe WP", sans-serif;
        font-size: 28px;
        font-weight: 100;
        margin-top: 5px;
        margin-bottom: 0px;}
    #index {
        border-collapse: separate; 
        border-spacing: 0; 
        margin: 0 0 20px; }
    #index th {
        vertical-align: bottom;
        padding: 10px 5px 5px 5px;
        font-weight: 400;
        color: #a0a0a0;
        text-align: center; }
    #index td { padding: 3px 10px; }
    #index th, #index td {
        border-right: 1px #ddd solid;
        border-bottom: 1px #ddd solid;
        border-left: 1px transparent solid;
        border-top: 1px transparent solid;
        box-sizing: border-box; }
    #index th:last-child, #index td:last-child {
        border-right: 1px transparent solid; }
    #index td.length, td.modified { text-align:right; }
    a { color:#1ba1e2;text-decoration:none; }
    a:hover { color:#13709e;text-decoration:underline; }
  </style>
</head>
<body>
  <section id="main">
    <header><h1>Index of <a href="/">/</a><a href="/Config/">Config/</a><a href="/Config/MainFolder/">MainFolder/</a><a href="/Config/MainFolder/SubFolder/">SubFolder/</a></h1></header>
    <table id="index" summary="The list of files in the given directory.  Column headers are listed in the first row.">
    <thead>
      <tr><th abbr="Name">Name</th><th abbr="Size">Size</th><th abbr="Modified">Last Modified</th></tr>
    </thead>
    <tbody>
      <tr class="file">
        <td class="name"><a href="File1.xml">File1.xml</a></td>
        <td class="length">10,117</td>
        <td class="modified">6/16/2023 12:08:00 PM</td>
      </tr>
      <tr class="file">
        <td class="name"><a href="File2.xml">File2.xml</a></td>
        <td class="length">10,782</td>
        <td class="modified">6/16/2023 12:08:00 PM</td>
      </tr>
      <tr class="file">
        <td class="name"><a href="File3.xml">File3.xml</a></td>
        <td class="length">5,359</td>
        <td class="modified">6/16/2023 12:08:00 PM</td>
      </tr>      
    </tbody>
    </table>
  </section>
</body>
</html>

Question

I know I can manually parse the index file to get a list of files.

However, I would like to know if there are any more robust / built-in functionality for getting a list of files in this circumstance.

It would be especially helpful if it could be done recursively.

0

There are 0 answers