Blazor.FileSystemAccess
Blazor.FileSystemAccess copied to clipboard
Feature: Local file access fully available only from code behind
@KristofferStrube Is it possible to select on one page let's say a whole main-directory path, and from a other page set additional folders in that main-directory and read/write/overwrite files into that directory automatically? So with automatically I mean by code, let's say by a event from example a http logic, but important not from a button click event or similar.
So is it possible to use it somehow similar like when you make a local app for example a console app in c# and using some file access like below, but in Blazor WASM?
// write code from backend code when a eventlistener has become a event (conceptual something like this) string localPath = @"C:\BlazorFolder\Log"; //in Blazor this master-path could also be selected when blazor starts, on a separate page
if( !Directory.Exists( localPath )) Directory.CreateDirectory( localPath ); if( !File.Exists( localPath + "log.txt") File.WriteAllText(localPath + "log.txt", string.empty); string readText = File.ReadAllText(localPath + "log.txt"); string newText = readText + "LOG-Message : stream service error detected " + DateTime.Now.ToString("MM/dd/yyyy hh:mm tt"); File.WriteAllText(localPath + "log.txt", newText );
If this is possible, could you integrate such a example in your project to demonstrate how this would work in Blazor WASM ?
Thank you!
Hey @vcarlucci,
Yes, this is possible. 😊 There isn't an equivalent method for Exists
but what you can do is to get all entries in a directory and check if your folder or file is one of these entries. This is especially easy if you use the FileSystemDirectoryHandleInProcess
variant.
I don't think this is a unique enough scenario for there to be added another sample for this, unless you have some fun/interesting case in mind. A somewhat equivalent example is what is done in this sample from the File System API: https://kristofferstrube.github.io/Blazor.FileSystem/SearchMastodon
Hi @KristofferStrube , thank you for your answer! I will have a look at this.
Yes I have an interesting case in mind 😊.. So actual I have a desktop WPF application with MVVM design pattern fortunally so I have the UI nicely separated from the backend libraries. In this desktop application, I have to use a lot of local file accesses which needs to be available also in Blazor. My goal is now, I would like to take the actual backend libraries from my existing desktop app and move all to Blazor, adapt the UI and then run all in Blazor webassembly. And if file accesses can be kept, theoretically this should all work.
To keep this migration somehow simple, I want to replace all actual functions with System.IO.Directory.xx and System.IO.File.xx with a specific Blazor "equivalent library" which I can simply replace this local directory and file functionalities.
Existing WPF desktop application uses:
System.IO.Directory.Exists(path) -> return bool System.IO.Directory.CreateDirectory(path) -> return exception if not successfull (authorization etc issue) System.IO.Directory.GetDirectories(path) -> return string[] of all subdirectories System.IO.Directory.GetFiles(path, ".json", SearchOption.AllDirectories) -> return string[] System.IO.Directory.GetFiles(path, ".json", SearchOption.TopDirectoryOnly) -> return string[]
System.IO.File.Exists(path) -> return bool System.IO.File.ReadAllText(path) -> return string System.IO.File.WriteAllText(path, content) -> overwrites existing file, when no file exists it creates it System.IO.File.Delete(path)
The coolest now would be, if I could simply replace System.IO with a Blazor specific library and it would work same as before ;)
@vcarlucci that sounds like an awesome project. If it is anything you can share then I would love to give it a look as well. Approaches for migrating to other platforms is always interesting to see and study.
I think a demo that showcases the parallel code for all methods and properties from the System.IO.File
and System.IO.Directory
namespaces would be very interesting. Actually I think this fits better into my Blazor.FileSystem wrapper.
A even more interesting project would be to build an abstraction of the interfaces in the Blazor File System API that extends and overrides the Directory
and File
types from the .NET standard library like you suggest.
I might get to that at some point. I'm currently on a ski vacation so my setup is limited, but I will give it a look once I am back.
@KristofferStrube I have not yet started with such a file system approach for Blazor. But yes that's exactly what would be really interesting I think. So enjoy your free time and skiing 😊