Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Essentials FileSystem can you save async

Tags:

xamarin

Looking into Xamarin essentials and wondering what the purpose of FileSystem is.

I was looking for something that would load and save a file for me async out of the box.

It looks to me and may be I am not understanding the usage that the only thing that it gives you is a location where to save and load the file and there is no functionality to actually save it for you eg "Old PCLStorage"

is this correct?

like image 276
developer9969 Avatar asked Jul 18 '18 04:07

developer9969


1 Answers

All Xamarin Essentials File System Helpers are doing is providing access to two different directories within your app, CacheDirectory and AppDataDirectory and access to the read-only pre-bundled files within application so you do not have to write platform-based code for these locations and do your own DI (or use Forms' DI) to access them...

Once you have the string-based directory location, then you use the normal .Net(Std) file and directory I/O routines. Create/delete subdirectories, create/read/write/delete a file using the async (or not) functions from the .Net(Std) framework or a third-party framework/package. Your choice...

CacheDirectory

The cache directory is a read/write location and it implements access to the "native" platform cache location. On Android this is the Cache

var cacheDir = FileSystem.CacheDirectory;

AppDataDirectory

The app data directory is the "default" location for where regular files should be stored.

As the docs state:

any files that are not user data file

FYI: This is not the place to be used for Sqlite databases and such if you are implementing/complying with platform dependent norms... Why Essentials did not include a database location is unknown to me when platforms like iOS and Android have APIs for them and formally documented locations... ;-/

Application bundled files (read-only)

OpenAppPackageFileAsync provides access (via a Stream) to read-only bundled files in your application (AndroidAssets, BundleResource, etc..)

using (var stream = await FileSystem.OpenAppPackageFileAsync("sushiLogo.png"))
{
    using (var reader = new StreamReader(stream))
    {
        var fileContents = await reader.ReadToEndAsync();
    }
}
like image 187
SushiHangover Avatar answered Oct 03 '22 06:10

SushiHangover