When it comes to file handling and Xamarin Forms you can find all you need in the official Documentation. However, when it comes to where the data should be stored the documentation leaves some points open. Moreover, might even lead to, dear I say it, your rejection in the App Store…
Also when writing Cross-Platform Code, with .Net Standard, it does depend on the Operating System (OS) that the app is being executed on, where to store your data. So let’s dive into the platforms which are primarily supported by Xamarin Forms.
The most comfortable way is using Xamarin.Essentials. Currently still in preview and requires Android 7.1 or higher. However, if that does not make you blink, these are the options:
You can access the folder paths as follows:
var rootDirectory = FileSystem.AppDataDirectory;
Currently Xamarin.Essentials supports Android, iOS and UWP. So if all the platforms your app requires are named, and you do not need any other location. Be sure to check out Xamarin.Essentials and check the documentation for more details.
In most cases the folders offered by Xamarin.Essentials will suffice, but if you require a different folder, i.e. the document folder under iOS, you can always set the path to the location on your own. So let’s have a look at how you can achieve this.
Now when it comes to Android, you can follow the documentation from Microsoft and use the following path for storing your files:
_rootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Looking to cache some files, then we can take the path and with Path.Combine set the path to the cache folder:
_cacheDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "cache");
If you want to store files to the SD-Card, i.e. external storage under Android. You have to pass in the path from a platform Project to the .Net Standard project or use multitargeting to achieve this. You can get the path as follows:
var sdCardPath = Environment.ExternalStorageDirectory.AbsolutePath;
Note that the Environment here is Android.OS.Environment and not System.Environment . So if Android is this easy how hard can iOS be?
When storing files under iOS, there is a bit more documentation to read. The reason being that Apple uses multiple subfolders within the Sandbox container. The ones that are important for file storage are the following:
For a more detailed listing check out the docs. So when storing files, you are under iOS this code will point to the Documents folder:
_rootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
If you intend to store information into the Library folder you can change the directory as follows:
_rootDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library");
Equally, you can change your path to one of the other locations described above.
UWP apps usually also store their data in a sandbox. Only if in the app’s metadata the permission is set and a good reason was given, which Microsofts validates on submission to the store, can the app access other file locations outside of the sandbox. UWP apps also live in a sandbox. The ApplicationData offers to store data locally, roaming or in a temporary location. The local folder can be accessed as follows:
_rootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
Similar the other locations can be accessed for example the roaming folder (which is synced automatically across all of your different devices):
_rootDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "..", "RoamingState");
The following directories are present in a UWP apps sandbox:
Since Xamarin Forms is no longer limited to Android, iOS and UWP, you might find yourself wanting to write files on another system such as Tizen. The best solution is to check the documentation of the given platform where data should be stored and then run the following code on the platform:
class Gnabber < // . private IEnumerableDirectoryDescriptions() < var specialFolders = Enum.GetValues(typeof(Environment.SpecialFolder)).Cast(); return specialFolders.Select(s => new DirectoryDesc(s.ToString(), Environment.GetFolderPath(s))).Where(d => !string.IsNullOrEmpty(d.Path)); > // . > class DirectoryDesc < public DirectoryDesc(string key, string path) < Key = key; Path = path == null ? "" : string.Join(System.IO.Path.DirectorySeparatorChar.ToString(), path.Split(System.IO.Path.DirectorySeparatorChar).Select(s =>s.Length > 18 ? s.Substring(0, 5) + ". " + s.Substring(s.Length - 8, 8) : s)); > public string Key < get; set; >public string Path < get; set; >>
The code above lists all used folders from the System.Environment.SpecialFolder for the given environment, and also provides you with the absolute path. If none of the special folders is the target location, you desired. Try using Path.Combine and a path nearby to get to your desired location.
Storing files is not tricky but put some thought into where to store your applications data can go a long way. Xamarin.Essentials may provide all the functionality you need, but if not you usually use the System.Environment.SpecialFolders and the System.Environment.GetFolderPath to get access to different folders offered by the platform you are on.
Updated: August 14, 2018
MAUI Reactor is a code first, MVU style framework based on .NET MAUI that promises less ceremony when writing your apps. And I like the sound of that and sin.
February 26, 2024
You might have an ASP.NET Core app that you deploy to IIS (Internet Information Server) that, for example, listens in the background to incoming IoT Signals .
December 31, 2023
Using Docker for your .NET builds gives you a reproducible way to execute your builds on your build server and developers’ devices. Setting up Docker to bu.
October 31, 2023
By discovering Tailscale, I finally found a solution for my QNAP NAS as a VPN gateway into my home network. I have a couple of devices at home that do not .