Blazor read directory

Viewed 8207

How do you read a content directory into Blazor.

I tried

Path.Combine(Directory.GetCurrentDirectory(), "..", "_posts");

But this throws an error.

Uncaught (in promise) Error: System.IO.DirectoryNotFoundException: Could not find a part of the path '/_posts'.
2 Answers

The answer depends on why you want to read the directory

  • If you want to interact with the user's file system, then you need to use the HTML standard for accessing the file system. A blazor wrapper is provided at https://github.com/Tewr/BlazorFileReader
  • If you want to retrieve files from your server, there are multiple approaches based on how you want to use it. You could set up an api, an mvc instance, or just serve static files. They'll all be relative to your server though, not the current directory.
  • If you want store data client-side, Flores made a good suggestion to use https://github.com/BlazorExtensions/Storage

Clientside Blazor is running in the same sandbox that javascript is running in. Which means your code has no way of accessing the local filesystem.

Maybe localStorage could be an alternative for you? There is a Blazor package to use it here: BlazorExtensions/Storage

Related