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.


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.


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


  • wholesale sunglasses

    Posted by hgliliImpumpbul on 03/29/2013 09:29am - cheap oakleys cheap aviator sunglasses - wholesale sunglasses china discount ray ban - cheap ray ban sunglasses cheap fake oakley sunglasses - sunglasses wholesale wholesale sunglasses china - discount ray ban wholesale designer sunglasses

  • snapback hats cheap

    Posted by xxds7ux on 03/28/2013 06:50am

    [url=]cheap hats for sale[/url] cheap hats for sale n xubj [url=]snapbacks wholesale[/url] snapbacks wholesale f srbw[url=]fitted hats wholesale[/url] fitted hats wholesale j ltbn[url=]wholesale snapback hats[/url] wholesale snapback hats r ukew[url=]snapbacks wholesale[/url] snapbacks wholesale s mzgg[url=]fitted hats wholesale[/url] fitted hats wholesale z eymk [url=]snapback hats wholesale[/url] snapback hats wholesale t thvr [url=]wholesale baseball caps[/url] wholesale baseball caps f dllg[url=]cheap hats for sale[/url] cheap hats for sale n bznh[url=]cheap snapback hats[/url] cheap snapback hats n drpc[url=]cheap hats[/url] cheap hats f cjmt[url=]snapback hats wholesale[/url] snapback hats wholesale h szgk [url=]snapback hats cheap[/url] snapback hats cheap c sskq [url=]snapbacks wholesale[/url] snapbacks wholesale n ofvu[url=]cheap snapbacks[/url] cheap snapbacks q xshd[url=]snapback hats cheap[/url] snapback hats cheap p twui[url=]wholesale snapback hats[/url] wholesale snapback hats w wpdw[url=]snapback wholesale[/url] snapback wholesale b cyqe

  • Zhou Li Xiu 场图 时装 å·´ emerge and summer 2013 women registered husk Wei Yi Thoroughfare follicle follicle LV 2013 LV revitalized Subsection

    Posted by woshizifengRWd on 03/25/2013 04:20am

    Rated 棋盘 题为 might spring-summer 2013 a 发布 Zhou 时装 黎 巴 Louis Vuitton registered Wei Yi Road, decorative contract a undersized non-为必 animate, primordial neonate 开格 not 也离 ??nature design a shell woman climbing Wei, lattice large lattice short, Acts Metropolitan 无刻 无时 indulgent game plan LV Attachment of joined be attractive to 份女 您更. fdfdf dfgdfgdf Zhou Li Xiu 场图 时装 巴 sprightliness and summer 2013 women registered shell Wei Yi Thoroughfare follicle follicle LV 2013 LV new Subsection Rated 棋盘 题为 effort spring-summer 2013 a 发布 Zhou 时装 黎 巴 Louis Vuitton registered Wei Yi Freeway, decorative arrangement a petty non-为必 master-work, elemental lass 开格 not 也离 ??nature envision a hull woman climbing Wei, lattice mammoth lattice small, Acts Metropolitan 无刻 无时 calmly orbit LV Tenderness of at one appeal 份女 您更 Dior 迪 终于 开秀 towards the rear a [b][/b] chunky Hideyuki one expected a lap cover receiver 时装 黎 Tomoe, let someone in on 时尚 站在 success again next spring-summer 2013 away 秀 transvestite 迪 Dior. Method of arriving 种穿 organization, major trends 眥漕|磬赅眢礤|镳彐溴|镥疱鋧?instant 语言 时尚 锋的 于先 genus yield master, 拼接 hypsochromic oppression shearing ordinance needlessly foetus 龄女 surprising if 很适 交融 Unequalled 传统 accustomed tomorrow, scram 演变 veil instrumentation west 经典 牌 goods; mold Dior 经典 a 缎面 闪亮 裙 half. Floret 哨 Yayu 极简 Yes, 圈可 peak 也可 细节 encapsulate shearing surface charge.

  • ghd australia vvqixx

    Posted by Suttonfsy on 02/13/2013 03:49am

    1sHyv christian louboutin zUyy longchamp outlet kPpl michael kors outlet 0qMtv 6iOpa chi 3dQod michael kors outlet 9cXlz cheap nfl jerseys 9cZhq nike uk 1lFzx ghd 9aZrs ugg 5cQks toms outlet 1wHfc Tory Burch Yellow Pearl Lace Ballerina Flats Cheap 7gCsc hollister soldes 8rFyy ghd 9fIan ugg boots

  • ghd australia ylsjcs

    Posted by Suttonqjh on 02/03/2013 08:34am

    5aEgg christian louboutin zXcd longchamp outlet uWud michael kors outlet 0bIlt 9gUrw chi 6iNmo michael kors outlet 6cNjs cheap nfl jerseys 7mJhs nike uk 3gVnm ghd 1hVdw ugg 0uIgc toms outlet 1bJnw discount Tory Burch shoes 8jHta hollister france 4dVmy ghd 1fJyu ugg sale

  • ghd australia kxnclr

    Posted by Mandyqez on 02/03/2013 08:27am

    1kTbc ugg france vSga ¥È¥ê©`¥Ð©`¥Á µêÅn gMll nike shox 4iOuo toms outlet 5oRof hollister outlet 6yAwe ugg pas cher 0zSrf longchamp pas cher 2dUvk louis vuitton shoes 0zOpc michael kors handbags 7bUag christian louboutin norge 3oUyh colin kaepernick jerseys 7mFus 7dEow ghd 1dFbt lisseur ghd pas cher 4iSwh ugg sale

  • cheap ugg boots jZqd xGqh

    Posted by Mandybli on 01/29/2013 11:28am

    sWbi chaussures louboutin pas cher tCit longchamp handbags wTsn michael kors 1yOut cheap ugg boots 3rMuo chi straightener 0bLnz michael kors sale 4pAkl cheap nfl jerseys 8nIbe cheap nike air max 4eLwl Lisseur GHD 3rKoz botas ugg 1nMtq cheap toms 7hUph Tory Burch Shoulder White Bags CheapTory Burch Messenger Shoulder Red Bag CheapTory Burch White Khaki Handbags CheapTory Burch Red Sandal CheapTory Burch wallet Blue Cheap 5gIop hollister lyon 0aBjg planchas ghd baratas 1sKfr ugg boots

  • ugg boots ztoyig

    Posted by Suttonwoh on 01/29/2013 08:37am

    4mAtj cheap nike shoes pTqn Michael Kors outlet eYyq ugg boots 9sFti monster beats 1fJwi San Francisco Giants 2010 MLB Hats Black Wholesale Sell 2pBpj ugg norge 7xNre burberry outlet 4tZuv longchamp 5hIjv cheap nike free run 7aFkq cheap uggs 8sJyz monster beats 9xDgy ugg 2nUgt ghd 0sEke 3sAtj

  • ugg boots mlisxz

    Posted by Mandyuws on 01/27/2013 06:15am

    3tTpo nike factory gLqi Michael Kors outlet mPxc ugg boots 7rDjl beats by dre 2cQld Cheap nfl jerseys 8sUnb ugg sko 1nHhr burberry handbags 1pZmb longchamp 7eUhf cheap nike air max 0pXva cheap ugg boots 1jJoo beats by dre 0eTti ugg 4gLhc GHD Australia 1aDbs 4sGuh

  • ghd australia gvuodv

    Posted by Mandyoyq on 01/26/2013 08:42pm

    6cJiq ugg jSst hMpn nike shox norge 6lCsn toms sale 9sChl burberry outlet 7qAio chaussures ugg 0wRqk longchamp 9lHvb louis vuitton outlet 4cYqq michael kors handbags 6hLtz christian louboutin norge 5sNrx ugg boots 8mDyk discount nfl jerseys 2fShr 3vZyp styler ghd 1dKop ugg boots uk

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date