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:
- 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;
- 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);
- Sometimes, we only need to store files temporarily. In such cases, we can make use of the cache location:
string cache = CacheDir.AbsolutePath;
Using the external storage only has a few extra requirements. Typically, the way external storage is used is the same as the way internal...