xamarin
  1. xamarin-working-with-files-and-storage

Xamarin Working with Files and Storage

Xamarin provides powerful APIs to work with File System and Storage. Developers can create, read, write, and delete files as well as save data locally on the device using different storage methods provided by Xamarin. In this article, we will learn about the different file system and storage options in Xamarin.

File System in Xamarin

Xamarin provides APIs to interact with different file systems on the device such as the internal storage and the external storage. The internal storage is private and can only be accessed by the application. The external storage is public and can be accessed by other applications and users.

Internal Storage

The internal storage is private and can only be accessed by the application. The default path for the internal storage is /data/data/package_name/.

Create a File

To create a file in the internal storage, use the following code:

string fileName = "myFile.txt";
string content = "Hello World";
string path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), fileName);

using (var stream = new System.IO.StreamWriter(path, true))
{
    stream.WriteLine(content);
}

Read a File

To read the contents of a file from the internal storage, use the following code:

string fileName = "myFile.txt";
string path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), fileName);

using (var stream = new System.IO.StreamReader(path))
{
    string content = stream.ReadToEnd();
}

External Storage

The external storage is public and can be accessed by other applications and users. The default path for the external storage is /storage/emulated/0/.

Check if External Storage is Available

To check if external storage is available on the device, use the following code:

bool isAvailable = Android.OS.Environment.ExternalStorageState == Android.OS.Environment.MediaMounted;

Create a File

To create a file in the external storage, use the following code:

const string FileName = "myFile.txt";
const string Content = "Hello World";

//This gets the path to the Documents directory for the app, which is part of external storage
var documentsDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var filePath = System.IO.Path.Combine(documentsDirectory, FileName);

using (var streamWriter = new System.IO.StreamWriter(filePath))
{
    streamWriter.Write(Content);
}

Read a File

To read the contents of a file from the external storage, use the following code:

const string FileName = "myFile.txt";

//This gets the path to the Documents directory for the app, which is part of external storage
var documentsDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var filePath = System.IO.Path.Combine(documentsDirectory, FileName);

using (var streamReader = new System.IO.StreamReader(filePath))
{
    var content = streamReader.ReadToEnd();
}

Storage Options in Xamarin

Apart from the file system, Xamarin also provides different storage options to store data locally on the device.

Application Preferences

Application preferences are used to store small amounts of data such as settings, user preferences, and other data specific to the application. It is stored in key-value pairs specific to the application.

Save Preferences

To save preferences, use the ISharedPreferences interface provided by Xamarin as follows:

ISharedPreferences preferences = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor editor = preferences.Edit();
editor.PutString("key", "value");
editor.Apply();

Read Preferences

To read preferences use the ISharedPreferences interface provided by Xamarin as follows:

ISharedPreferences preferences = PreferenceManager.GetDefaultSharedPreferences(this);
string value = preferences.GetString("key", null);

SQLite

SQLite is a self-contained, serverless, zero-configuration, transactional SQL database engine. It provides a lightweight database solution that is accessible and manageable directly from a device.

Create Database

To create a database file in Xamarin, use the following code:

var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "mySQLite.db3");
var db = new SQLiteConnection(path);
db.CreateTable<MyTable>();

Save Data

To save data to the SQLite database in Xamarin, use the following code:

var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "mySQLite.db3");
var db = new SQLiteConnection(path);
db.Insert(myObject);

Retrieve Data

To retrieve data from the SQLite database in Xamarin, use the following code:

var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "mySQLite.db3");
var db = new SQLiteConnection(path);
var myObjects = db.Table<MyTable>().ToList();

Summary

Xamarin provides various storage options for developers to store data locally on the device. Developers can interact with the file system of the device and also use application preferences and SQLite databases to store data. By leveraging these storage options, developers can build feature-rich mobile applications that store data locally on the device.

Published on: