Archives for 23 May,2016

You are browsing the site archives by date.

Simple Cross-Platform File IO for iOS, Android, and Windows

One of the biggest challenges in Cross Platform Mobile App Development is implementing a mechanism of performing simple tasks on each diverse platform, such as reading a config file on app launch, when you know that each platform will store this file in a different location, have a preference for different file formats, and have different permission mechanisms for reading and writing. This plugin helps nail most of those issues.

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

The 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 IFolder and IFile interfaces. When creating folders and files, we are also given maximum control over collision detection preferences with theCreationCollisionOption 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.

1

2

3

4

5

6

7

8

9

10

11

// 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:

1

2

3

4

5

// Actually, this wasn’t a selfie of just me!

await selfiePhotoFile.RenameAsync(“groupSelfie.png”);

// It’s a horrible selfie anyways, let’s delete it!

await selfiePhotoFile.DeleteAsync();

Wrapping Up

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.

Source: Simple Cross-Platform File IO for iOS, Android, and Windows | Xamarin Blog