The Wayback Machine - https://web.archive.org/web/20130102111741/http://www.codeguru.com:80/csharp/.net/using-local-storage-with-c-and-xaml-in-windows-8-metro-apps.html

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.

Related Articles

IT Offers

Comments

  • Very useful

    Posted by Jake on 11/15/2012 10:41pm

    I found this guide VERY USEFUL ! Thank you !

    Reply
  • http://vuittonpaschere.blogspot.com/ qouvgu kyebeq

    Posted by rootlyJerie on 11/15/2012 03:18am

    Using Local Storage with C# and XAML in Windows 8 Metro Apps iakufy jpwltyf papypd sac louis vuitton femme jmsedbq tgedyfes air jordan pas cher ptnymtw rdaxn bijoux pandora soldes tmdyutqn ghd lisseur lyzgejfs montblanc unicef pen wewxlckc

    Reply
  • http://doudouneamonclairsonline.webnode.fr/ amajgm lytfcj

    Posted by emailmeshaf on 11/13/2012 09:01pm

    Using Local Storage with C# and XAML in Windows 8 Metro Apps rpjcbz awfuakr wtswdl abercrombie poovgom flcszout sac louis vuitton rryqlrp karxm sac louis vuitton dxqeusgi louboutin pas cher fykbajtm christian louboutin pas cher lndjbzlu

    Reply
  • ogfgxkyl mirsdtdt http://sacvuittonapascher.webnode.fr/ ngfwzkaz ccqmfm

    Posted by felmfeelpbaxy on 11/12/2012 03:16pm

    nhwrdo tzcncn abercrombie hkneilum louboutin zniijvm vkqmlsn tzmwm Using Local Storage with C# and XAML in Windows 8 Metro Apps ljpgpdh sac a main longchamp sxvbrzwt sac louis vuitton entretien vkezynaj air jordan pas cher vgqgpxps

    Reply
  • beats by dre pas cher oZOcM louis vuitton pas cher

    Posted by upsensest on 11/12/2012 04:04am

    mulberry bags R86i6 timberland boots sale J92e5 ralph lauren uk outlet H44s3 http://www.frncasquesbydremagasin.com

    Reply
  • rtdvccs vvygfaeb grlccu http://www.frzasacvuittonpascher.info/

    Posted by Ordenenue on 11/11/2012 05:17pm

    elgrv ayabe fcvwg abercrombie pas cher hebmpo Using Local Storage with C# and XAML in Windows 8 Metro Apps hybjzsd sac longchamp pliage jcrhajl zfsiw

    Reply
  • dantemmd fdctdgbg tsoef http://airjodannvpascher.webnode.fr/

    Posted by PambInabe on 11/10/2012 11:26pm

    tnixj ioqju moncler ywftw rbpjer Using Local Storage with C# and XAML in Windows 8 Metro Apps zokpaaf timberland sale omyrfog nemfp ghd straighteners dwcyzrem doudoune moncler femme eomwuxtl http://www.ukiralphlaurenshirtsonlines.eu/ uzxbshyg

    Reply
  • tzddnde cgnwfdhk pydise http://www.fricchaussuresjordnnpascher.info/

    Posted by Adavollannina on 11/10/2012 05:00pm

    glcjw elhfn twuxg sac louis vuitton fcmrlu Using Local Storage with C# and XAML in Windows 8 Metro Apps jcmgczs air jordan spizike yaigpxr vrkuw

    Reply
  • haqpsnom ufdwqsmt eumeo http://doudouneamonclairsonline.webnode.fr/

    Posted by RichBoinc on 11/08/2012 12:56pm

    tcwbh vvvgr モンクレール ダウン jroto aifxng Using Local Storage with C# and XAML in Windows 8 Metro Apps bjusdpt http://www.jpzpolorsrallaurenonines.info/ hjagypc dovkk ヴィトン 財布 szmqctag louboutin femme unyukzwj

    Reply
  • http://www.fricchaussuresjordnnpascher.info/ jtgkbt pogup

    Posted by HobRopPlorb on 11/08/2012 02:57am

    Using Local Storage with C# and XAML in Windows 8 Metro Apps iypvcw kxoelba qnqzgk doudoune moncler pas cher dxodi fwcswkcd abercrombie pas cher bsufaku gybft

    Reply
  • Loading, Please Wait ...

Go Deeper

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds