Most mobile applications need to interact with the underlying file system. Be it building a database or caching data, some understanding of how file systems work on target platforms is required. If you’re working with multiple platforms, not only does this require understanding of how each individual file system works, but also how to work with the file system from shared code.
The File System Plugin for Xamarin and Windows reduces the underlying file system complexities for each platform into a cross-platform file IO API for iOS, Android, and Windows, making it extremely easy to work with the file system from shared code. In this blog post, you will learn how to use the File System Plugin for Xamarin and Windows to create, edit, and delete files and directories from shared code.
Introduction to the File System Plugin for Xamarin and Windows
Similar to desktop operating systems, mobile operating systems each have their own file system. Building applications that target multiple operating system requires knowledge of not just one file system, but the underlying file system for each platform. Another barrier when working with file systems is the inability to use code that talks to individual file systems in shared code. A common solution to this problem is to use preprocessor directives (#ifdefs) to access platform-specific features, but this won’t work with PCLs and results in messier code.
Plugins for Xamarin expose platform-specific functionality via a cross-platform API that can be consumed from a Portable Class Library or Shared Project, such as using device geolocation, sending an SMS, or storing app settings, to help make you share even more code and increase developer productivity. The File System Plugin for Xamarin and Windows makes working with the many different mobile file systems easy with a shared, cross-platform API. You can download the plugin using the Xamarin Component Store or via the NuGet Package Manager.
Exploring the APIs
IFileSystem interface represents an abstracted file system at the highest level. The platform-specific implementation can be accessed via the
FileSystem.Current property. The file system is made up of a collection of folders and individual files, which are abstracted via the
IFile interfaces. When creating folders and files, we are also given maximum control over collision detection preferences with the
CreationCollisionOption enumeration, which allows us do everything from create an alternative name, replace the existing directory/file, open the existing directory/file, or throw an exception. Of course, all of these APIs are also async/await compatible as well.
// Access the file system for the current platform.
IFileSystem fileSystem = FileSystem.Current;
// Get the root directory of the file system for our application.
IFolder rootFolder = fileSystem.LocalStorage;
// Create another folder, if one doesn't already exist.
IFolder photosFolder = await rootFolder.CreateFolderAsync("Photos", CreationCollisionOption.OpenIfExists);
// Create a file, if one doesn't already exist.
IFile selfiePhotoFile = await photosFolder.CreateFileAsync("selfie.png", CreationCollisionOption.ReplaceExisting);
Renaming and deleting files is also super easy as well:
// Actually, this wasn't a selfie of just me!
// It's a horrible selfie anyways, let's delete it!
In this blog post, you learned how to interact with native file systems for iOS, Android, and Windows from shared code using the File System Plugin for Xamarin and Windows. To learn more about Plugins for Xamarin or check out other plugins available, such as geolocation, messaging, and sharing, check out our complete plugin directory. Visit the plugin in the Xamarin Component Store for more documentation or view the source code online on GitHub.