Using Local Storage with C# and XAML in Windows 8 Metro Apps

As you dive into development of Windows 8 Metro apps you will most likely run into the need to use local storage. While you may be thinking it’s just Windows, I already know how to connect to a database and access the filesystem, unfortunately, the APIs you are used too will not work with Metro apps. Metro apps work on a very different model, whereby each app is restricted to a sandbox.

Working within a sandbox allows for much better control and prevents one app from affecting another. Unfortunately, this also means that you will not be able to use the System.Data namespace to connect to a local and/or a SQL Server on your network. Instead you will need to provide a set of Web Services or other means of accessing remote data as if it were coming from a server on the web. While this may seem counter-intuitive to traditional Windows apps, it makes sense for Metro apps working within a sandbox model.

Local and Roaming Storage

Nonetheless, your app will have the need to store data locally on the machine. Metro supports three different types of storage, which are specific to your app, including Local, Roaming and Temporary. Files stored within the Local folder will only be stored on the machine in which they were created. Unlike the Local folder, the Roaming folder allows for data to be synced between Windows 8 machines running your Metro app. The Temporary folder works as you would expect whereby data stored within the folder will be deleted periodically by Windows. In addition to accessing the file system, you also have the ability to store Local Settings and Roaming Settings, which allow you to store a key/value pair information for your app.

To jump in and start using local storage, you will be using the Windows.Storage.ApplicationData class. The following code snippet is a very simple example of how to create a new file in local storage and write text to it.

async void save_myFile()
{//Get the Local Foldervar localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

//Create the File
StorageFile myFile = await localFolder.CreateFileAsync("myFile.txt", CreationCollisionOption.ReplaceExisting);

//Write File to the file
await FileIO.WriteTextAsync(myFile, "Write Test Data!");}

Right off the bat the first line async modifier in the method declaration is new. This modifier is used to tell Metro to allow this method to be executed asynchronously, which is used to help keep the main UI thread responsive while background code is executing. Next we create a local variable, which points to the Local Folder for this application. Then using the local folder we create a new file called myFile.txt. Notice the await expression, which allows the method to pause while the create file is executing. Once complete, we use the FileIO class and the WriteTextAsync to write a single line of text to the file. Using this method allows you to write text to the file without the need to perform open/close operations. If instead you want to use the Roaming Folder the same way, you could replace LocalFolder in the first line with RoamingFolder.

Settings

Taking advantage of storage works a little bit differently; however, it is very intuitive. The following line of code shows how you can add a setting called test and provide a value to it and retrieve the setting.

//Set test setting
ApplicationData.Current.LocalSettings.Values["test"] = "Setting Value";

//Read test setting
string t = (string)ApplicationData.Current.LocalSettings.Values["test"];

Again you can take advantage of RoamingSettings by switching from LocalSettings to RoamingSettings.

Conclusion

As you can see, it is very easy to take advantage of Local and Roaming storage and settings. At this point, you may be thinking that your app is going to need a small local database such as SQL CE, SQLite, etc. Unfortunately, out of the box Metro does not include a local database; however, due to the popularity of SQLite, several different projects are currently underway to port SQLite over to Metro. In addition, you may be able to get away without the need for a high-level query language such as SQL. Depending on your app requirements you may be able to use a local XML file and/or a JSON (JavaScript Object Notation) formatted file. Nonetheless, Metro does include the basic tools necessary to store local files and settings.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read