Using files and the filesystem

Many apps require access to the filesystem for accessing databases, reading content, and many other reasons.

How to do it...

There are two main areas for storing files: internal storage and external storage. Making use of the internal storage or app sandbox is very easy:

  1. Writing files to the filesystem is very simple, and all that is required is the path to the sandbox for our app:
    string sandbox = FilesDir.AbsolutePath;
  2. Once we have this path, we can use the types from the .NET BCL to manipulate the files:
    string file = Path.Combine(sandbox, "myFile.txt");
    bool exists = File.Exists(file);
    File.WriteAllText(file, "this is my value");
    string value = File.ReadAllText(file);
  3. Sometimes, we only need to store files temporarily. ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.