5

I'm trying to get a list of the static files inside of my wwwroot folder in a Blazor WebAssembly project. This is what I've tried so far:

string[] images = Directory.GetFiles(@"wwwroot\images", "*.jpg");

I get this error:

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
  Unhandled exception rendering component: Could not find a part of the path '/wwwroot\images'.

System.IO.DirectoryNotFoundException: Could not find a part of the path '/wwwroot\images'.

What am I doing wrong? Thank you.

Dustuu
  • 77
  • 2
  • 8
  • Does this answer your question? [What is the equivalent of Server.MapPath in ASP.NET Core?](https://stackoverflow.com/questions/49398965/what-is-the-equivalent-of-server-mappath-in-asp-net-core) – Gusman Jun 27 '20 at 21:57
  • Unfortunately, no. This only applies to Blazor Server- I am using Blazor WebAssembly. – Dustuu Jun 27 '20 at 22:05
  • I don't think you can run this code in Blazor WebAssembly App. I know that if you want to read the content of a json file located in the wwwroot folder, you should use HTTP calls. But to retrieve the names of the files can be only done, I guess, using JSInterop. Try JSInterop, perhaps you'd succeed. – enet Jun 27 '20 at 23:05

2 Answers2

5

My first thought was that Directory.GetFiles() shouldn't be supported on Blazor WebAssembly - you are not allowed on the filesystem.
But running System.IO.Directory.GetDirectories(".") gives this output:

./tmp
./home
./dev
./proc
./zoneinfo

which are some pre-packaged files&folders you can get at. But none of them contain wwwroot and the contents you're after.

So you can use Http.GetStreamAsync() or Http.GetByteArrayAsync() to get the contents of an image file you already know the name of. There is no direct way to scan a folder: a security feature. (I know you can configure ISS to allow 'browsing' a folder but that goes against best practices and gives you HTML to parse).

If you do want to scan a folder, build an API to do so. You need to run that on the server.

H H
  • 263,252
  • 30
  • 330
  • 514
  • So if you're running a Blazor wasm standalone (not hosted) app, there's no way to enumerate the `content-root` or `web-root` paths? – HMZ Sep 16 '20 at 16:09
  • No, not that I know of. And that is right, there is no web-root path after launch. That is a server thing. Ask an API when you need something. – H H Sep 16 '20 at 17:25
  • 3
    I am creating a static blog with blazor wasm and the only way that worked for me was to construct a `Json` metadata file with a prebuild script and requesting it with the `HttpClient` in order to keep track of file count, dates and other metadata. hopefully this will be easier in the future. – HMZ Sep 16 '20 at 17:32
  • It is a little easier now, see https://github.com/KristofferStrube/Blazor.FileSystemAccess – H H Jun 12 '23 at 13:37
-1

Please get "images" directory path like below:

string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images");
string[] images = Directory.GetFiles(folderPath, "*.jpg");

Hope it will work.

  • 1
    Has this solution been tested? First of all, it stands in contradiction to the previous answer. Furthermore, also in 2022 it does not seem to work. – JAL Oct 19 '22 at 06:09