Windows 8 – Copy file from Assets to Local Folder

When porting mobile apps to Windows 8, I immediately came across the same issue as each of the previous platforms, namely, how to copy my ‘starting’ data files into local storage where they can be modified.

In this case, I have a file in my Assets folder called ‘Prayers.xml’. At application startup I want to verify if the file already exists in the local store – if not, copy it from the Assets folder in the installation directory.

This is how you end up handling it in Windows 8.

        async void CopyPrayerFile()
        {
            //get the storage for your app 
            Windows.Storage.StorageFolder store = Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile prayerFile = null;
            try
            {
                prayerFile = await store.GetFileAsync("Prayers.xml");
            }
            catch (System.IO.FileNotFoundException)
            {}

            if (prayerFile == null)
            {
                //get the file from Assets

                StorageFolder install = Windows.ApplicationModel.Package.Current.InstalledLocation;
                StorageFile installFile = await install.GetFileAsync("Assets\Prayers.xml");

                installFile.CopyAsync(store);   
            }
        }